爬虫---练习源码

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

复制代码
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)
相关推荐
深蓝电商API35 分钟前
大模型 + 爬虫 = ?我用 AI 做了一个自适应反反爬引擎
人工智能·爬虫
༒࿈南林࿈༒2 小时前
国家医保局 API 加密体系逆向全记录——SM2签名 + SM4加解密 + SHA256 头签名
爬虫·大模型应用·mcp·skills
跨境数据猎手2 小时前
复刻Cssbuy跨境淘宝代购集运系统搭建方案
爬虫·架构·系统架构
郑洁文7 小时前
基于网络爬虫的XSS漏洞检测系统的设计与实现
网络·爬虫·网络安全·xss
Super Scraper8 小时前
如何将赋予千问(Qwen Code)网络检索功能:集成MCP服务器
人工智能·爬虫·ai·自动化·千问·mcp·qwen code
SilentSamsara8 小时前
爬虫工程化:Playwright + 反反爬 + 数据清洗管道实战
开发语言·爬虫·python·青少年编程·playwright
专注VB编程开发20年11 小时前
Python爬虫、提取网页内容,免费调用谷歌翻译接口
爬虫·python·信息可视化
Data 实验室11 小时前
TaskPyro爬虫管理平台 v2.3.4:脚本即接口,调度即编排
爬虫
小白学大数据11 小时前
全站链接深度爬取:Python GUI 事件绑定 + 运行时动态过滤实现思路
开发语言·爬虫·python
绘梨衣54721 小时前
某爬虫策略的基础skills
爬虫·ai编程