python request+正则爬取百度贴吧狗吧上的标题(200页)

本文介绍了一种使用Python的requests库和正则表达式从百度贴吧抓取帖子标题的方法。通过设置User-Agent并循环请求不同页数的贴吧页面,解析出每个帖子的标题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

https://rifngah.github.io/-requests-/   这是一些其他笔记。
import requests
import re
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3724.8 Safari/537.36'

}
i=0
while(i<1000):
    response = requests.get("http://tieba.baidu.com/f?kw=%E7%8B%97&ie=utf-8&pn={}".format(i),
                        headers=headers)
    i+=5
    response.encoding = response.apparent_encoding
    t_concleafix = re.findall(r'<a\s+rel="noreferrer".*?</a>', response.content.decode())
    # print(t_concleafix)
    for i in range(0, t_concleafix.__len__()):
        # print(t_concleafix[i])
        gro = re.search(
            r'<a\s+rel="noreferrer"\s+href=".*?"\s+title="(.*?)"\s+target="_blank"\s+class="j_th_tit\s+">.*?</a>',
            t_concleafix[i])
        if(gro):
            print(gro.group(1))

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值