爬虫---练习源码

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

复制代码
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)
相关推荐
过往入尘土11 小时前
网络爬虫的相关知识和操作
爬虫
Elastic 中国社区官方博客12 小时前
在 Windows 上使用 Docker 运行 Elastic Open Crawler
大数据·windows·爬虫·elasticsearch·搜索引擎·docker·容器
平凡灵感码头12 小时前
用 urllib 开启爬虫之门:从零掌握网页数据抓取
爬虫
小关会打代码15 小时前
Python编程进阶知识之第二课学习网络爬虫(requests)
爬虫·python·学习
wadesir18 小时前
Python获取网页乱码问题终极解决方案 | Python爬虫编码处理指南
开发语言·爬虫·python
羊八井19 小时前
使用 Playwright 构建小红书笔记采集:从架构设计到实战应用
爬虫·python
q567315233 天前
使用aiohttp实现高并发爬虫
javascript·css·爬虫·python
q567315234 天前
Selenium动态网页爬虫编写与解释
爬虫·selenium·测试工具
是小崔啊4 天前
【爬虫】02 - 静态页面的抓取和解析
爬虫
捉鸭子5 天前
转转APP逆向
爬虫·python·网络安全·网络爬虫