爬虫---练习源码

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

复制代码
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)
相关推荐
Caco.D3 小时前
Aneiang.Pa.News:属于你自己的全平台热点聚合阅读器
爬虫·asp.net·aneiang.pa·热榜新闻
小白学大数据10 小时前
Java 异步爬虫高效获取小红书短视频内容
java·开发语言·爬虫·python·音视频
我想吃烤肉肉10 小时前
Python 中 asyncio 是什么?
爬虫·python·自动化
@杨某15 小时前
超级鹰的使用
爬虫·selenium
小白学大数据15 小时前
百科词条结构化抓取:Java 正则表达式与 XPath 解析对比
java·开发语言·爬虫·正则表达式
泡泡以安17 小时前
【爬虫教程】第4章:HTTP客户端库深度定制(httpx/aiohttp)
爬虫·http·httpx
搞全栈小苏17 小时前
【Python 爬虫实战】一键批量下载 Python 官网 FTP 所有版本全部文件(多线程 + 断点续传 + 禁用系统代理 完整版)
开发语言·爬虫·python
泡泡以安17 小时前
【爬虫教程】第5章:WebSocket协议解析与长连接管理
爬虫·websocket·网络协议
laocooon52385788617 小时前
《网络爬虫实战:小说内容抓取》教案
爬虫
亿牛云爬虫专家1 天前
Worker越简单,系统越稳定:从单机到集群
爬虫·python·集群·爬虫代理·单机·代理ip·worker