用最简单爬虫爬取图片

简单代码即可爬取图片:

#1.发送请求
#2活动响应
#3解析数据
#4存储数据
import requests,re
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=1584796650335_R&pv=&ic=&nc=1&z=&hd=&latest=&copyright=&se=1&showtab=0&fb=0&width=&height=&face=0&istype=2&ie=utf-8&hs=2&ctd=1584796650337%5E00_1266X150&sid=&word=%E6%B1%BD%E8%BD%A6%E5%9B%BE%E7%89%87'
headers={#字典 伪装
    'Referer':'https://image.baidu.com/search/index?tn=baiduimage&ct=201326592&lm=-1&cl=2&ie=gb18030&word=%C6%FB%B3%B5%CD%BC%C6%AC&fr=ala&ala=1&alatpl=adress&pos=0&hs=2&xthttps=111111'
}
#访问目标网站
response=requests.get(url=url,headers=headers)
#print(response.text)
data_text=response.text
image_urls=re.findall('middleURL":"(https://ss3.*?\.jpg)',data_text)
#设置计数器
n=0;
#遍历图片的链接
for image_url in image_urls:
    n=n+1
    #重新发起图片请求
    res=requests.get(url=image_url,headers=headers)
    #res中已经有图片数据---二进制数据
    #使用Python的open()函数进行存储
    with open('%d.jpg'%n,'wb') as f:
        f.write(res.content)




 

爬取结果如下:

 

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值