# -*- coding: utf-8 -*-
"""
json加载一页30个,学习找瀑布流的方法。
"""
import requests
import re
headers={
        'Referer':'https://image.baidu.com/search/index?',
        'User-Agent':'Mozilla/5.0 (Windows NT 6.1; ) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/82.0.4068.4 Safari/537.36'
        }
keyword=input('your keyword:')
depth=int(input('your page number:'))
for i in range(1,depth+1):
    url='https://image.baidu.com/search/acjson?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&hd=&latest=&copyright=&word={}&s=&se=&tab=&width=&height=&face=0&istype=2&qc=&nc=1&fr=&expermode=&force=&cg=girl&pn={}&rn=30&gsm=5a&1584946135692='.format(keyword,keyword,i*30)
    response=requests.get(url,headers=headers)
    url_real=re.findall('"thumbURL":"(.*?)"',response.text)
    for x in url_real:
        con=requests.get(x,headers=headers).content
        #十六进制图片内容print(con)
        path=x.split('/')[-1]
        with open('image/'+path,'wb') as f:#二进制要加b
            f.write(con)
            print('{} saved'.format(x))