python 爬虫 下载百度美女图片
因为要从网上下载很多图片,一张一张的复制下载速度很慢。
- 爬虫实现方式
- 查找到访问图片的链接URI
- 访问URI获取到图片的链接
- 访问图片的链接,并保存图片到本地
废话不多说 上代码
import requests
import json
def get_image_url():
url = 'https://image.baidu.com/search/index?tn=resulttagjson&logid=10358070151245603719&ie=utf-8&fr=&word=%E7%BE%8E%E5%A5%B3%E5%9B%BE%E7%89%87&ipn=r&fm=index&pos=history&queryWord=%E7%BE%8E%E5%A5%B3%E5%9B%BE%E7%89%87&cl=2&lm=-1&oe=utf-8&adpicid=&st=-1&z=&ic=0&hd=1&latest=©right=&s=&se=&tab=&width=&height=&face=0&istype=2&qc=&nc=1&expermode=&nojc=&isAsync=true&pn=0&rn=30&gsm=5a&1713426180895='
headers = {
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36 Edg/123.0.0.0"
}
response = requests.get(url, headers=headers)
download_image = dict()
if response.status_code == 200:
data = response.text
json_data = json.loads(data)['data']
for image in json_data:
if image:
download_image[image['fromPageTitle']] = image['thumbURL']
return download_image
def download(image,title):
response = requests.get(image)
with open('image/' + title + '.jpg', mode="bw") as f:
f.write(response.content)
if __name__ == '__main__':
download_image = get_image_url()
for title, image in download_image.items():
title = title.replace('"','').replace('|','').replace('/','')
download(image, title)
- 找到下载图片的文件夹,哈哈 美女图片可以独自欣赏啦
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步