python 异步下载图片_python3抓取异步百度瀑布流动态图片(二)get、json下载代码讲解...
制作解析網址的get
1 defgethtml(url,postdata):2
3 header = {'User-Agent':4 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0',5 'Referer':6 'http://image.baidu.com',7 'Host': 'image.baidu.com',8 'Accept': 'text/plain, */*; q=0.01',9 'Accept-Encoding':'gzip, deflate',10 'Accept-Language':'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',11 'Connection':'keep-alive'}12
13 #解析網頁
14 html_bytes = requests.get(url, headers=header,params =postdata)15
16 return html_bytes
頭部的構造請參考上一篇博文:
分析網址:
http://image.baidu.com/search/acjson?tn=resultjson_com&ipn=rj&ct=201326592&is=&fp=result&queryWord=gif&cl=2&lm=-1&ie=utf-8&oe=utf-8&adpicid=&st=-1&z=&ic=0&word=gif&s=&se=&tab=&width=&height=&face=0&istype=2&qc=&nc=1&fr=&pn=30&rn=30&gsm=1e&1472364207674=
分解為:
url = 'http://image.baidu.com/search/acjson?' + postdata + lasturl
lasturl為時間戳,精確到后三位小數的時間戳,構造這個時間戳,后三位小數我就隨機生成一個三位數了:
1 importtime2 importrandom3 timerandom = random.randint(100,999)4 nowtime =int(time.time())5 lasturl = str(nowtime) + str(timerandom) + '='
最后制作postdata:
1 #構造post
2 postdata ={3 'tn':'resultjson_com',4 'ipn':'rj',5 'ct':201326592,6 'is':'',7 'fp':'result',8 'queryWord': keyword,9 'cl': 2,10 'lm': -1,11 'ie': 'utf-8',12 'oe': 'utf-8',13 'adpicid': '',14 'st': -1,15 'z':'',16 'ic': 0,17 'word': keyword,18 's': '',19 'se': '',20 'tab': '',21 'width': '',22 'height': '',23 'face': 0,24 'istype': 2,25 'qc': '',26 'nc': 1,27 'fr': '',28 'pn': pn,29 'rn': 30,30 'gsm': '1e'
31 }
其中頁數pn和搜索關鍵字keywork為:
1 #搜索的關鍵字
2 #keywork = input('請輸入你要查找的關鍵字')
3 keyword = 'gif'
4
5 #頁數
6 #pn = int(input('你要抓取多少頁:'))
7 pn = 30
將得到的信息保存在本地,當所有都保存下來了再去下載圖片:
1 #解析網址
2 contents =gethtml(url,postdata)3
4 #將文件以json的格式保存在json文件夾
5 file = open('../json/' + str(pn) + '.json', 'wb')6 file.write(contents.content)7 file.close()
讀取文件夾里面的所有文件:
1 #找出文件夾下所有xml后綴的文件
2 def listfiles(rootdir, prefix='.xml'):3 file =[]4 for parent, dirnames, filenames inos.walk(rootdir):5 if parent ==rootdir:6 for filename infilenames:7 iffilename.endswith(prefix):8 file.append(rootdir + '/' +filename)9 returnfile10 else:11 pass
遍歷json文件夾,讀取里面的東西:
1 #找到json文件夾下的所有文件名字
2 files = listfiles('../json/', '.json')3 for filename infiles:4 print(filename)5 #讀取json得到圖片網址
6 doc = open(filename, 'rb')7 #('UTF-8')('unicode_escape')('gbk','ignore')
8 doccontent = doc.read().decode('utf-8', 'ignore')9 product = doccontent.replace(' ', '').replace('\n', '')10 product = json.loads(product)
查詢字典data:
# 得到字典data
onefile = product['data']
將字典里面的圖片網址和圖片名稱放到數組里面:
制作一個解析頭來解析圖片下載:
1 defgetimg(url):2
3 #制作一個專家
4 opener =urllib.request.build_opener()5
6 #打開專家頭部
7 opener.addheaders = [('User-Agent',8 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0'),9 ('Referer',10 'http://image.baidu.com'),11 ('Host', 'image.baidu.com')]12 #分配專家
13 urllib.request.install_opener(opener)14
15 #解析img
16 html_img =urllib.request.urlopen(url)17
18 return html_img
最后將圖片下載到本地的gif文件夾:
1 for item inonefile:2 try:3 pic = getimg(item['thumbURL'])4 #保存地址和名稱
5 filenamep = '../gif/' + validateTitle(item['fromPageTitleEnc'] + '.gif')6 #保存為gif
7 filess = open(filenamep, 'wb')8 filess.write(pic.read())9 filess.close()10
11 #每一次下載都暫停1-3秒
12 loadimg = random.randint(1, 3)13 print('圖片' + filenamep + '下載完成')14 print('暫停' + loadimg + '秒')15 time.sleep(loadimg)16
17 exceptException as err:18 print(err)19 print('暫停' + loadimg + '秒')20 time.sleep(loadimg)21 pass
得到效果如下:
本文只是編程,處理這種網址最重要的是思想,思想我寫在上一篇博文:
思想有了,程序是很簡單的問題而已。
總結
以上是生活随笔為你收集整理的python 异步下载图片_python3抓取异步百度瀑布流动态图片(二)get、json下载代码讲解...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysqlclient==1.3.7对应
- 下一篇: python 图像处理_Python中的