爬虫---练习源码

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

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)
相关推荐
莓事哒1 小时前
静态网页的爬虫(以电影天堂为例)
爬虫·python
q567315231 小时前
使用CPR库编写的爬虫程序
开发语言·爬虫·golang·音视频
HerrFu1 小时前
可狱可囚的爬虫系列课程 18:成都在售新房数据爬虫(lxml 模块)实战
爬虫·python
m0_748236581 小时前
Python数据分析案例30——中国高票房电影分析(爬虫获取数据及分析可视化全流程)
爬虫·python·数据分析
张高兴1 小时前
张高兴的大模型开发实战:(一)使用 Selenium 进行网页爬虫
爬虫·python·selenium·大模型
九丶黎3 小时前
爬虫案例十一js逆向数位观察网
开发语言·javascript·爬虫
q567315235 小时前
用Haskell语言和wreq库配合HTTP写个爬虫程序
爬虫·网络协议·http
Java开发-楠木11 小时前
【js逆向】某预约票网站 (webpack技术)
爬虫
SRC_BLUE_1715 小时前
[网络爬虫] 动态网页抓取 — Selenium 入门操作
网络·爬虫·selenium·测试工具
愚公搬代码1 天前
【愚公系列】《Python网络爬虫从入门到精通》045-Charles的SSL证书的安装
网络·爬虫·python·网络协议·ssl