爬虫---练习源码

选取的是网上对一些球员的评价,来评选谁更加伟大一点

复制代码
import csv
import requests
import re
import time

def main(page):
    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)
    for u,c,t in zip(users,comments,comment_times):
        # 筛选数据,过滤掉异常数据
        if 'img' in c or 'div' in c or len(u)>50:
            continue
        csvwriter.writerow((u,t,c))
        print(u,t,c)
    print(f'第{page}页爬取完毕')

if __name__ == '__main__':
    with open('01.csv','a',encoding='utf-8')as f:
        csvwriter = csv.writer(f)
        csvwriter.writerow(('评论用户','评论时间','评论内容'))
        for page in range(1,8):  # 爬取前7页的内容
            main(page)
            time.sleep(2)
相关推荐
傻啦嘿哟3 小时前
长效住宅代理IP:反爬虫战场上的隐形盾牌
爬虫·网络协议·tcp/ip
华科云商xiao徐5 小时前
冷门但好用的Python库写个爬虫代码
爬虫
华科云商xiao徐5 小时前
Koa+Puppeteer爬虫教程页面设计
爬虫
Python×CATIA工业智造8 小时前
列表页与详情页的智能识别:多维度判定方法与工业级实现
爬虫·深度学习·pycharm
失败又激情的man19 小时前
python之requests库解析
开发语言·爬虫·python
爬虫程序猿19 小时前
利用爬虫按关键字搜索淘宝商品实战指南
android·爬虫
打酱油的;19 小时前
爬虫-数据解析
爬虫
打酱油的;19 小时前
爬虫-request处理get
爬虫·python·django
布语world1 天前
2025快手创作者中心发布视频python实现
爬虫·python
浏览器API调用工程师_Taylor1 天前
Look my eyes 都2025年了,你还不会将重复的事情自动化?
前端·javascript·爬虫