Python爬虫系列-爬取百度贴吧图片

这是我新开的一个博客系列-Python爬虫,里面收集了我写过的一些爬虫脚本给大家参考,水平有限,不当之处请见谅。

这是我之前在CSDN问答贴中回答网友的问题:

(https://ask.csdn.net/questions/8042566?spm=1001.2014.3001.5505)

网友给了基础版,但是有问题,爬不出图片,我在他的基础上加入了header参数可以下载了。具体见如下源码:

python 复制代码
# 百度贴吧的图片下载
# 1.通过request拿到源代码数据
# 2.通过bs对源代码进行解析,拿到图片的urL
# 3.依次对图片地址发送请求
# 4.把图片内容写进文件中

import requests
from bs4 import BeautifulSoup as bs

header = {'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0',
          'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
          'Accept-Encoding':'gzip, deflate, br',
          'Accept-Language':'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
          'Host':'tieba.baidu.com'
          }
index_url = 'https://tieba.baidu.com/p/8783217764'
res= requests.get(url=index_url, headers=header)
soup = bs(res.content,"html.parser")
image_urls = soup.select("img.BDE_Image")
# print(response.content)

offset = 0
for image_url in image_urls:
    print(image_url['src'])
    image_content = requests.get(image_url['src']).content
    with open('{}.jpg'.format(offset), 'wb')as f:
        f.write(image_content)
    offset = offset + 1

效果如下:

相关推荐
等一朵映山红1 分钟前
动态图 vs 静态图:PyTorch 与 TensorFlow 架构底层对比
人工智能·pytorch·python
卷无止境25 分钟前
Python 依赖管理这件事,到底该看哪个文件
后端·python
在世修行32 分钟前
从零打造 C# 工业视觉检测系统(十一):Task 异步编程与实时性保障
开发语言·c#·task异步
卷无止境35 分钟前
FastAPI 缓存方案全解析:从内存到分布式的工程实践
后端·python
烟漠河洛36 分钟前
一条走廊,两张地图
python
苏灿烤鱼37 分钟前
GitHub #2 拆解|仅 +59,代码知识图谱为什么仍值得关注?
python·agent·mcp
讲温控就好了39 分钟前
数据中心热密度飙升下的超精密温控应对策略
人工智能·python
金融小师妹8 小时前
多因子智能推演:油价回落6%与黄金震荡上行的关联解析——AI预测框架
大数据·python·深度学习
BUG研究员_10 小时前
Runnable与LCEL
开发语言·人工智能·python