1. 程式人生 > >爬取網頁瀑布流圖片

爬取網頁瀑布流圖片

import requests
from urllib import request
url = "https://image.baidu.com/search/acjson?tn=resultjson_com&ipn=rj&ct=201326592&is=&fp=result&queryWord=%E8%A5%BF%E6%B8%B8%E8%AE%B0&cl=2&lm=-1&ie=utf-8&oe=utf-8&adpicid=&st=-1&z=&ic=0&word=%E8%A5%BF%E6%B8%B8%E8%AE%B0&s=&se=&tab=&width=&height=&face=0&istype=2&qc=&nc=1&fr=&expermode=&pn=30&rn=30&gsm=1e&1540450027225="

headers = {
"Referer": "https://image.baidu.com/search/index?tn=baiduimage&ipn=r&ct=201326592&cl=2&lm=-1&st=-1&fm=result&fr=&sf=1&fmq=1540449575028_R&pv=&ic=0&nc=1&z=&se=1&showtab=0&fb=0&width=&height=&face=0&istype=2&ie=utf-8&word=%E8%A5%BF%E6%B8%B8%E8%AE%B0",
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36"
}

data = """tn: resultjson_com
ipn: rj
ct: 201326592
is:
fp: result
queryWord: 科邁羅
cl: 2
lm: -1
ie: utf-8
oe: utf-8
adpicid:
st: -1
z:
ic: 0
word: 科邁羅
s:
se:
tab:
width:
height:
face: 0
istype: 2
qc:
nc: 1
fr:
expermode:
pn: 30
rn: 30
gsm: 1e
1540450027225:"""

send= {}
for line in data.split("\n"):
line_data = line.split(": ")
if len(line_data) == 2:
key,value = line_data
if key and value:
send[key] = value
response = requests.get(url = url,headers = headers, params = send)

content = response.json()["data"]

for index,src in enumerate(content):
index = str(index).zfill(2)
img_url = src.get("middleURL")
if img_url:
name = "image/%s_%s.jpg"%("科邁羅",index)
try:
request.urlretrieve(url = img_url,filename = name)
except Exception as e:
print(e)
else:
print("%s is down"%name)