爬虫---练习源码

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

复制代码
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)
相关推荐
sugar椰子皮9 分钟前
【爬虫框架-2】funspider架构
爬虫·python·架构
APIshop3 小时前
用“爬虫”思路做淘宝 API 接口测试:从申请 Key 到 Python 自动化脚本
爬虫·python·自动化
xinxinhenmeihao20 小时前
爬虫如何使用代理IP才能不被封号?有什么解决方案?
爬虫·网络协议·tcp/ip
2501_938810111 天前
什么IP 适用爬虫 采集相关业务
爬虫·网络协议·tcp/ip
第二只羽毛2 天前
主题爬虫采集主题新闻信息
大数据·爬虫·python·网络爬虫
0***h9422 天前
初级爬虫实战——麻省理工学院新闻
爬虫
是有头发的程序猿2 天前
Python爬虫实战:面向对象编程在淘宝商品数据抓取中的应用
开发语言·爬虫·python
Onebound_Ed2 天前
Python爬虫进阶:面向对象设计构建高可维护的1688商品数据采集系统
开发语言·爬虫·python
深蓝电商API2 天前
爬虫登录态维护高级技巧:Cookie 池 + Session 复用实战
爬虫
嫂子的姐夫2 天前
01-selenium
爬虫·python·selenium·自动化