爬虫---练习源码

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

复制代码
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)
相关推荐
深蓝电商API4 小时前
常见反爬虫策略与破解方案汇总
爬虫
(时光煮雨)11 小时前
【Python进阶】Python爬虫-Selenium
爬虫·python·selenium
嫂子的姐夫11 小时前
10-七麦js扣代码
前端·javascript·爬虫·python·node.js·网络爬虫
深蓝电商API16 小时前
将爬虫部署到服务器:Scrapy+Scrapyd 实现定时任务与监控
服务器·爬虫·scrapy
nightunderblackcat17 小时前
四大名著智能可视化推演平台
前端·网络·爬虫·python·状态模式
数据牧羊人的成长笔记21 小时前
python爬虫scrapy框架使用
爬虫·python·scrapy
Fairy_sevenseven1 天前
[1]python爬虫入门,爬取豆瓣电影top250实践
开发语言·爬虫·python
white-persist2 天前
MCP协议深度解析:AI时代的通用连接器
网络·人工智能·windows·爬虫·python·自动化
深蓝电商API2 天前
不止是 Python:聊聊 Node.js/Puppeteer 在爬虫领域的应用
爬虫·python·node.js
摩羯座-185690305942 天前
爬坑 10 年!京东店铺全量商品接口实战开发:从分页优化、SKU 关联到数据完整性闭环
linux·网络·数据库·windows·爬虫·python