爬虫2 urllib3 爬取30张百度图片
Posted cxhzy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了爬虫2 urllib3 爬取30张百度图片相关的知识,希望对你有一定的参考价值。
import urllib3 import re # 下载百度首页页面的所有图片 # 1. 找到目标数据 # page_url = ‘http://image.baidu.com/search/index?tn=baiduimage&ct=201326592&lm=-1&cl=2&ie=gb18030&word=%CD%BC%C6%AC&fr=ala&ala=1&alatpl=others&pos=0‘ # http = urllib3.PoolManager() # res = http.request(‘get‘,page_url) # print(res.data.decode(‘utf-8‘)) # Ajax的 ajax_url = ‘http://image.baidu.com/search/acjson?tn=resultjson_com&ipn=rj&ct=201326592&is=&fp=result&queryWord=%E5%9B%BE%E7%89%87&cl=2&lm=-1&ie=utf-8&oe=utf-8&adpicid=&st=&z=&ic=&hd=&latest=©right=&word=%E5%9B%BE%E7%89%87&s=&se=&tab=&width=&height=&face=&istype=&qc=&nc=&fr=&expermode=&force=&pn=30&rn=30&gsm=1e&1546957772498=‘ http = urllib3.PoolManager() res = http.request(‘get‘,ajax_url) # print(res.data.decode()) img_urls = re.findall(r‘"thumbURL":"(.*?),‘,res.data.decode()) # print(img_urls) # print(len(img_url)) headers = { ‘Referer‘:‘https://www.baidu.com/s?ie=utf-8&wd=%E5%9B%BE%E7%89%87‘ } for i , img_url in enumerate(img_urls): # print(img_url) img = http.request(‘get‘,img_url,headers=headers)