爬虫---练习源码

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

复制代码
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)
相关推荐
CrawlerCracker2 小时前
小程序逆向|六六找房|请求头Authorization
javascript·爬虫·python·小程序·网络爬虫·js
天天扭码4 小时前
零基础入门 | 超详细讲解 | 小白也能看懂的爬虫程序——爬取微博热搜榜
前端·爬虫·cursor
水w7 小时前
【Python爬虫】简单案例介绍3
开发语言·爬虫·python·scrapy·beautifulsoup
攻城狮7号9 小时前
Python爬虫第14节-如何爬取Ajax的数据
爬虫·python·python爬虫
cloudy4919 小时前
Java爬虫:HtmlUnit爬取基金排名,AI分析数据(DeepSeek,Dify)
人工智能·爬虫
水w17 小时前
【Python爬虫】简单案例介绍1
开发语言·爬虫·python
水w1 天前
【Python爬虫】简单介绍
开发语言·爬虫·python·beautifulsoup
大神薯条老师1 天前
Python高级爬虫之JS逆向+安卓逆向1.4节:数据运算
爬虫·python·机器学习·数据分析·网络爬虫
q567315231 天前
使用Scrapy库结合Kotlin编写爬虫程序
爬虫·scrapy·kotlin
水w1 天前
【Python爬虫】简单介绍2
开发语言·爬虫·python·beautifulsoup