写了一个简单的爬图片练习
import requests import os def bd_img(title1): url = f'https://image.baidu.com/search/acjson?tn=resultjson_com&logid=10755979809828115852&ipn=rj&ct=201326592&is=&fp=result&fr=&word={title1}&queryWord={title1}&cl=2&lm=-1&ie=utf-8&oe=utf-8&adpicid=&st=-1&z=&ic=0&hd=&latest=©right=&s=&se=&tab=&width=&height=&face=0&istype=2&qc=&nc=1&expermode=&nojc=&isAsync=&pn=60&rn=30&gsm=3c&1693994902388=' headers = { 'Cookie': 'BIDUPSID=4D5BFD2C8ECBF122BA1A0F786EC0383B; PSTM=1690680395; BAIDUID=5689C24BF18912C47F7B8AFF8FFEB272FG=1; MCITY=-356%3A; BDUSS=VZTVZuemFoSllLT25SS0d3Z2pRQXhzfmRyLTRvanlWY3NFU0tmbDdTVTQ5QWhsRVFBQUFBJCQAAAAAAAAAAAEAAADpE9JKwfTD-7rDwvAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADhn4WQ4Z-FkZm; BDUSS_BFESS=VZTVZuemFoSllLT25SS0d3Z2pRQXhzfmRyLTRvanlWY3NFU0tmbDdTVTQ5QWhsRVFBQUFBJCQAAAAAAAAAAAEAAADpE9JKwfTD-7rDwvAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADhn4WQ4Z-FkZm; BDORZ=FFFB88E999055A3F8A630C64834BD6D0; H_PS_PSSID=26350; BAIDUID_BFESS=5689C24BF18912C47F7B8AFF8FFEB272', 'Referer': 'https//www.baidu.com/', 'Upgrade-Insecure-Requests': '1', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36 Edg/116.0.1938.69' } img = requests.get(url,headers=headers).json().get('data') if not os.path.exists('./bdimg'): os.mkdir('./bdimg') num = 1 for i in img: img_url = i.get('thumbURL') if img_url: img_re = requests.get(img_url, headers=headers) with open('./bdimg/{}_{}.png'.format(title1,num),'wb') as f: f.write(img_re.content) print("下载完成") num +=1 if __name__ == '__main__': title1 = input("请输入图片名称:") bd_img(title1)
标签:__,img,headers,python,get,爬虫,title1,url,百度 From: https://www.cnblogs.com/fanpush/p/17687300.html