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

效果如下:

相关推荐
xb113214 小时前
C# WinForms界面设计
开发语言·c#
-Rane14 小时前
【C++】内存管理
开发语言·c++
深蓝电商API14 小时前
Scrapy杜绝重复请求:Rfpdupfilter源码分析与优化
爬虫·python·scrapy
DARLING Zero two♡14 小时前
【计算机网络】简学深悟启示录:序列化&&反序列化
开发语言·计算机网络·php
ID_1800790547314 小时前
乐天(Letian)商品详情API接口的调用示例与代码实现
开发语言·python
南 阳15 小时前
Python从入门到精通day10
linux·windows·python
mftang15 小时前
Python 获取当前目录的多种方法
python
一位搞嵌入式的 genius15 小时前
深入理解 JavaScript 原型与继承:从基础到进阶
开发语言·前端·javascript
晨非辰15 小时前
C++波澜壮阔40年|类和对象篇:拷贝构造与赋值重载的演进与实现
运维·开发语言·c++·人工智能·后端·python·深度学习
多米Domi01115 小时前
0x3f 第36天 外卖8,9,树
数据结构·python·算法·leetcode