第一次写爬虫
小白的不得了
import urllib.request
from uuid import uuid4
import requests
import re
import time
head={
'Referer':'https://image.baidu.com/search/index?tn=baiduimage&ipn=r&ct=201326592&cl=2&lm=-1&st=-1&sf=1&fmq=&pv=&ic=0&nc=1&z=&se=1&showtab=0&fb=0&width=&height=&face=0&istype=2&ie=utf-8&fm=index&pos=history&word=%E7%99%BD%E4%B8%8A%E5%90%B9%E9%9B%AA',
'Mozilla/5.0':'(Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'
}
def main():
for i in range(30,9999,30):
try:
url="https://image.baidu.com/search/acjson?tn=resultjson_com&ipn=rj&ct=201326592&is=&fp=result&queryWord=%E5%88%9D%E9%9F%B3&cl=2&lm=-1&ie=utf-8&oe=utf-8&adpicid=&st=-1&z=0&ic=&hd=&latest=©right=&word=%E5%88%9D%E9%9F%B3&s=&se=&tab=&width=0&height=0&face=0&istype=2&qc=&nc=&fr=&expermode=&force=&pn="+str(i)+"&rn=30&gsm=1e&1590159062284="
res = requests.get(url).json()
#print(res)
jiexi(res)#
except:
pass
def jiexi(res):
html=res
data = html['data']
for i in data:
try:
img = i['middleURL']
#print(img)
xiaz(img)
except:
pass
def xiaz(img):#下载图片
mc=str(uuid4())+".jpg"
print(mc)
f = requests.get(img)
with open(mc,"wb") as code:
code.write(f.content)
if __name__=='__main__':
main()
print("下载完毕")