import requests
import os
import time
m = 1
p = int(input(“请输入你要爬取的页数:”))
for n in range (1,p+1):
#通过抓包获取url
url = “http://image.baidu.com/search/acjson?tn=resultjson_com&ipn=rj&ct=201326592&is=&fp=result&queryWord=%E8%A1%A8%E6%83%85&cl=2&lm=-1&ie=utf-8&oe=utf-8&adpicid=&st=&z=&ic=&word=%E8%A1%A8%E6%83%85&s=&se=&tab=&width=&height=&face=&istype=&qc=&nc=1&fr=&pn={}&rn=30&gsm=186&1534833478899=”.format(n*60)
headers = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36","Referer":"http://image.baidu.com/search/index?tn=baiduimage&ps=1&ct=201326592&lm=-1&cl=2&nc=1&ie=utf-8&word=%E8%A1%A8%E6%83%85","Host":"image.baidu.com",'Accept-Language':"en-US,en;q=0.9","Accept-Encoding":"gzip, deflate","Accept":"text/plain, */*; q=0.01"}
#请求url,得到参数
response = requests.request("get",url,headers=headers)
#格式,不然会乱码
response.encoding = "utf-8"
#获取图片的个数
b = len(response.json()['data'])-2
for i in range(b):
# imgurl = (response.json()['data'][i]['middleURL'])
# print(imgurl)
# imgurl=response.json()['data'][i]['middleURL']
# print (imgurl)
#请求图片的而连接
data = requests.get(response.json()['data'][i]['middleURL'])
# print(response.json()['data'][i]['middleURL'])
# print(response)
#检测文件夹,如果没有就去创建
folder = os.path.exists("E:\\img\\")
if not folder:
os.makedirs("E:\\img\\")
else:
print ("========================")
#保存图片
with open ("E:\\img\\"+str(m)+'.jpg','wb') as f:
# print(m)
f.write(data.content)
print("开始下载:"+"第{}张".format(m))
time.sleep(1)
m+=1