下面是代码
import requests
import time
import json
class Image(object):
def __init__(self):
self.url = "https://image.baidu.com/search/acjson?"
self.header = {
"Cookie": "BDqhfp=%E7%8B%97%26%260-10-1undefined%26%2612968%26%267; BIDUPSID=A9057507F795F3B64AAD1B3C8EEE77F4; PSTM=1628755996; BAIDUID=A9057507F795F3B68C23CF2841217265:FG=1; __yjs_duid=1_6b5e03d0f7ee9d96564f91318a17c3f61629164474262; BDSFRCVID_BFESS=IykOJexroG0YRbTHbwHVKreH-9NbUdrTDYrEQ-mAp1wm6V8VJeC6EG0Pts1-dEu-EHtdogKKBmOTHnuF_2uxOjjg8UtVJeC6EG0Ptf8g0M5; H_BDCLCKID_SF_BFESS=tR3j3Ru8KJjEe-Kk-PnVeUF-MPnZKRvHa2kjhxotQpnDfpnKyn5qyKr-Xx5PKnjn3N5HKxDEalj2ht3zjlDW3xI8LNj405OTbTADsRbNb66pO-bghPJvyUDDXnO72JQlXbrtXp7_2J0WStbKy4oTjxL1Db3JKjvMtgDtVJO-KKChbKPwjf5; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; H_PS_PSSID=34440_34145_31254_34551_33848_34525_34585_34092_34106_26350_34428_34556; delPer=0; PSINO=2; BAIDUID_BFESS=D9039E16E07D4F59A09083EAD9E25BBB:FG=1; BA_HECTOR=a1200h058k21a48kd41gjvkgd0q; BDRCVFR[X_XKQks0S63]=mk3SLVN4HKm; userFrom=www.baidu.com; firstShowTip=1; indexPageSugList=%5B%22%E7%8B%97%22%5D; cleanHistoryStatus=0; BDRCVFR[dG2JNJb_ajR]=mk3SLVN4HKm; ab_sr=1.0.1_NzRlNmE0M2VjMzU2NWQ2YjU5Yjk4NjQ2ZWRhZjEwZTE5MjJiYTBkNzJlODk3MDM0NmUwMjg5M2YzZmUwZTY0YWMwYWI1M2Y3OGVlMjFhZDBkZjU2YWQwOTMyOWVmOTlhYjgxMGJhNjViYzk1NDk1MWNlN2EyN2NkOTkwOWJmMjRmMmIwZGZhNzdlMGNlMTJiOWQ4MTZkZjkxYWU1ZDZhYg==; BDRCVFR[-pGxjrCMryR]=mk3SLVN4HKm",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36 Edg/93.0.961.47"
}
self.params = {
" n": "resultjson_com",
" logid": "8234466765640172309",
"ipn": "rj",
"ct": "201326592",
"is": "",
"fp": "result",
"queryWord": "狗",
"cl": "2",
"lm": "-1",
"ie": "utf-8",
"oe": "utf-8",
"adpicid": "",
"st": "-1",
"z": "",
"ic": "0",
"hd": "",
"latest": "",
"copyright": "",
"word": "狗",
"s": "",
"se": "",
"tab": "",
"width": "",
"height": "",
"face": "0",
"istype": "2",
"qc": "",
"nc": "1",
"fr": "",
"expermode": "",
"nojc": "",
"pn": "180",
"rn": "30",
"gsm": "3c",
"time": ""
}
self.image_list = []
def get_image(self, num):
for i in range(0, num):
self.params["time"] = int(time.time() * 1000)
self.params["pn"] = i * 30
response = requests.get(url=self.url, headers=self.header, params=self.params)
for j in range(0, len(response.json()["data"])-1):
self.image_list.append(response.json()["data"][j]["thumbURL"])
def save_image(self):
n = 1
for i in self.image_list:
image = requests.get(url=i)
with open("./图片/{}.jpg".format(n), "wb") as f:
f.write(image.content)
n += 1
if __name__ == '__main__':
image = Image()
image.get_image(3)
image.save_image()