嗨喽~大家好呀,这里是魔王呐
百度搜图是大家常用得一个找图小窝点,
里面包含了各种地方得各种图片,如果你不会找图,去百度搜图准没错~
今天我们就来采集一下百度图片叭~
本篇代码提供者: 青灯教育-巳月老师
python 3.8
运行代码
pycharm 2021.2
辅助敲代码
requests
win + R 输入 cmd 点击确定, 输入安装命令 pip install 模块名 (pip install requests) 回车
在pycharm中点击Terminal(终端) 输入安装命令
import requests
headers = {
'Host': 'image.baidu.com',
'Cookie': 'BDqhfp=%E9%BB%91%E4%B8%9D%26%26NaN-1undefined%26%263540%26%268; BIDUPSID=C24C7D8E598E67C686237DEAF51F7B28; PSTM=1656683846; BDUSS=FgtVzQyZHE4QnliUkVmTjdldE5VOEdtNHlVQldZb2xLWVVJZnFYOGRWcHRsZVppRVFBQUFBJCQAAAAAAAAAAAEAAAD36OLxx-C1xr3M0~0AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAG0Iv2JtCL9id; BDUSS_BFESS=FgtVzQyZHE4QnliUkVmTjdldE5VOEdtNHlVQldZb2xLWVVJZnFYOGRWcHRsZVppRVFBQUFBJCQAAAAAAAAAAAEAAAD36OLxx-C1xr3M0~0AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAG0Iv2JtCL9id; BAIDUID=90512AA3632B152E5F977142A71CD0B0:SL=0:NR=10:FG=1; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; BAIDUID_BFESS=90512AA3632B152E5F977142A71CD0B0:SL=0:NR=10:FG=1; BA_HECTOR=2k0k2k8l848k8ha5811hc5i8j15; ZFY=2fgqyju9wf05Hbo:AJBrnR:BLWCyBQUITGRJI5nCwvvEg:C; H_PS_PSSID=36545_36462_36721_36455_36668_34812_36691_36167_36693_36696_36073_36772_36746_36760_36771_36766_26350_36712; delPer=0; PSINO=6; BDRCVFR[dG2JNJb_ajR]=mk3SLVN4HKm; userFrom=www.baidu.com; BDRCVFR[-pGxjrCMryR]=mk3SLVN4HKm; ab_sr=1.0.1_MzZhYWQzYThjYWRjNGQ5NDdhOGMxZGYwOTMzZGM5ZDYxYThlYjgyNjg0YjkwZjU1ZDcyZTJiNWFhNmE5YWE5MjMzMzA2Y2NlODg4MDFjZDkyZjljNDYzYTVmZjE5OGMyMDNiODdlMTE3MTliYjgyODg2OWUyMDhiNTczZWFhZjQ3ZWFjMDhmNGViODdkYjhmNjY1MjdlYWNhODlhOTEzMA==',
'Referer': 'https://image.baidu.com/search/index?tn=baiduimage&ps=1&ct=201326592&lm=-1&cl=2&nc=1&ie=utf-8&dyTabStr=MCwzLDIsMSw0LDUsOCw3LDYsOQ%3D%3D&word=%E9%BB%91%E4%B8%9D',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36',
}
number = 1
for page in range(1, 11):
url = f'https://image.baidu.com/search/acjson?tn=resultjson_com&logid=7395480229139350733&ipn=rj&ct=201326592&is=&fp=result&fr=&word=%E9%BB%91%E4%B8%9D&queryWord=%E9%BB%91%E4%B8%9D&cl=2&lm=-1&ie=utf-8&oe=utf-8&adpicid=&st=&z=&ic=&hd=&latest=©right=&s=&se=&tab=&width=&height=&face=&istype=&qc=&nc=1&expermode=&nojc=&isAsync=&pn={page * 30}&rn=30&gsm=d2&1657006313320='
response = requests.get(url=url, headers=headers)
json_data = response.json()
data_list = json_data['data']
for data in data_list[:-1]:
fromPageTitleEnc = data['fromPageTitleEnc']
middleURL = data['middleURL']
print(fromPageTitleEnc,middleURL)
img_data = requests.get(middleURL).content
with open(f'img/{number}.jpg', mode='wb') as f:
f.write(img_data)
number += 1
解答、教程可加Q:261823976免费获取哦~
也可以直接查看文章下方推广加助理小姐姐V免费获取呐~
对啦~要记得在代码文件相同地方新建一个名称为img
得文件夹哦
不然会报错得呐~
成功没有快车道,幸福没有高速路。
所有的成功,都来自不倦地努力和奔跑,所有的幸福都来自平凡的奋斗和坚持
——励志语录
本文章就写完啦~感兴趣的小伙伴可以复制代码去试试
你们的支持是我最大的动力!!记得三连哦~ 欢迎大家阅读往期的文章呀~