参考:
分享Python7个爬虫小案例(附源码)_爬虫案例-优快云博客
获取html文本
url = f'https://tieba.baidu.com/p/7882177660?pn={page}' headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36(KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36'} resp = requests.get(url, headers=headers) html = resp.text
获取页面数据
# 评论内容 comments = re.findall('style="display:;">(.*?)</div>', html) # 评论用户 users = re.findall('class="p_author_name j_user_card" href=".*?" target="_blank">(.*?)</a>', html) # 评论时间 comment_times = re.findall('楼</span><span class="tail-info">(.*?)</span><div', html)