爬虫---练习源码

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

复制代码
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)
相关推荐
huzhongqiang3 小时前
Python全站链接爬取工具优化:支持过滤和断点续爬
后端·爬虫
李松桃6 小时前
Python爬虫-实战
爬虫·python
跨境数据猎手6 小时前
B 站 item_search_video 接口开发,搭建生产级视频搜索服务
大数据·爬虫·python
小白学大数据17 小时前
Python 自动化爬取网易云音乐歌手歌词实战教程
爬虫·python·okhttp·自动化
深蓝电商API17 小时前
京东API批量操作优化:单次1000条限制的突破方案
爬虫·接口·api·京东api
Python大数据分析@1 天前
浏览器自动化工具 Selenium,Playwright,Puppeteer 做爬虫有哪些弊病?
爬虫·selenium·自动化
剑神一笑1 天前
从零开始理解 robots.txt:搜索引擎爬虫的“门禁系统“
爬虫·搜索引擎
捉鸭子2 天前
某音a_bogus vmp逆向
爬虫·python·web安全·node.js·js
Python大数据分析@3 天前
CLI一键采集,使用Python搭建TikTok电商爬虫Agent
开发语言·爬虫·python
编程隐士3 天前
爬虫管理系统实现方案
爬虫