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

效果如下:

相关推荐
2601_949146532 分钟前
C语言语音通知API示例代码:基于标准C的语音接口开发与底层调用实践
c语言·开发语言
开源技术13 分钟前
Python Pillow 优化,打开和保存速度最快提高14倍
开发语言·python·pillow
学嵌入式的小杨同学18 分钟前
从零打造 Linux 终端 MP3 播放器!用 C 语言实现音乐自由
linux·c语言·开发语言·前端·vscode·ci/cd·vim
Li emily1 小时前
解决港股实时行情数据 API 接入难题
人工智能·python·fastapi
wfeqhfxz25887821 小时前
农田杂草检测与识别系统基于YOLO11实现六种杂草自动识别_1
python
mftang2 小时前
Python 字符串拼接成字节详解
开发语言·python
0思必得02 小时前
[Web自动化] Selenium设置相关执行文件路径
前端·爬虫·python·selenium·自动化
石去皿2 小时前
大模型面试通关指南:28道高频考题深度解析与实战要点
人工智能·python·面试·职场和发展
jasligea2 小时前
构建个人智能助手
开发语言·python·自然语言处理
kokunka2 小时前
【源码+注释】纯C++小游戏开发之射击小球游戏
开发语言·c++·游戏