python爬取百度搜索图片

我完整具体步骤

1.请求标头,伪装浏览器身份
2.确定请求的路径,根据路径获得网页源代码的html文件
3.解析html,获得对应的图片地址,再一次请求图片地址,保存到本地

构造请求头,伪装浏览器,(这里只用ua伪装的话会被挡)

headers={
    'Accept-Encoding':'gzip, deflate, br',
    'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-',
    'Accept-Language':'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0'

}

找到目标图片所处的位置

 

完整代码,不断爬取图片,可手动暂停 

import requests
import re
import os
import time

headers={
    'Accept-Encoding':'gzip, deflate, br',
    'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-',
    'Accept-Language':'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0'

}

url = 'https://image.baidu.com/search/index?tn=baiduimage&ipn=r&ct=201326592&cl=2&lm=-1&st=-1&fm=result&fr=&sf=1&fmq=1701837432375_R&pv=&ic=&nc=1&z=&hd=&latest=&copyright=&se=1&showtab=0&fb=0&width=&height=&face=0&istype=2&dyTabStr=MCwyLDMsMSw2LDQsNSw3LDgsOQ%3D%3D&ie=utf-8&sid=&word=%E9%A3%8E%E6%99%AF%E5%9B%BE%E7%89%87'
html = requests.get(url=url,headers=headers)
html.encoding = 'utf-8'
html = html.text

img_url_list = re.findall('"objURL":"(.*?)"',html)

img_path = "./nature/"
if not os.path.exists(img_path):
    os.mkdir(img_path)
count=0
for i in img_url_list:
    img = requests.get(i)
    count+=1
    print("正在爬取第", count, '张')
    file_name = f'{img_path}图片{str(count)}.jpg'
    f = open(file_name, 'wb')
    f.write(img.content)
    time.sleep(0.5)

查看爬取结果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值