【爬虫】对某某贴吧主页的爬虫分析+源码

1. 网站分析

  想要的内容有标题、时间和帖子跳转链接

查看网站源代码,发现想要的内容就在里面,那就好办了,直接上正则,当然beautifulsoup也不是不可以

2. Python源码

import requests
import re
from prettytable import PrettyTable

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/114.0'
}
x = PrettyTable(["标题", "时间", "链接"])
x.align["标题"] = "l"
// 这里会整合三页内容,想要多少页,就把101改成50*(页数-1)+1
for i in range(0, 101, 50):
    resp = requests.get(f'https://tieba.baidu.com/f?kw=%E5%8D%9A%E5%B0%94%E7%89%B9&ie=utf-8&pn={i}', headers=headers)
    with open(file='1.html', mode='w', encoding='utf-8') as f:
        f.write(resp.text)
    obj = re.compile('<a rel="noopener" href="/p/(?P<url>.*?)" title="(?P<title>.*?)".*?<span class="pull-right is_show_create_time" title="创建时间">(?P<time>.*?)</span>', re.S)
    title = obj.finditer(resp.text)
    for i in title:
        x.add_row([i.group('title'), i.group('time'), 'https://tieba.baidu.com/p/' + i.group('url')])
print(x)

 3. 效果展示

ps:好久没玩爬虫了,但是在网页版逛吧时看到很多广告让我很不爽,故写下此脚本。

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值