【爬虫实战】python微博热搜榜Top50

一.最终效果

二.项目代码

2.1 新建项目

本文使用scrapy分布式、多线程爬虫框架编写的高性能爬虫,因此新建、运行scrapy项目3步骤:

1.新建项目:
scrapy startproject weibo_hot

2.新建 spider:
scrapy genspider hot_search "weibo.com"

3.运行 spider:
scrapy crawl hot_search

注意:hot_search 是spider中的name

4.编写item:

py 复制代码
class WeiboHotItem(scrapy.Item):

    index = scrapy.Field()
    topic_flag = scrapy.Field()
    icon_desc_color = scrapy.Field()
    small_icon_desc = scrapy.Field()
    small_icon_desc_color = scrapy.Field()
    is_hot = scrapy.Field()
    is_gov = scrapy.Field()
    note = scrapy.Field()
    mid = scrapy.Field()
    url = scrapy.Field()
    flag = scrapy.Field()
    name = scrapy.Field()
    word = scrapy.Field()
    pos = scrapy.Field()
    icon_desc = scrapy.Field()

5.编写爬虫解析代码:

python 复制代码
import os
from itemadapter import ItemAdapter
from .settings import DATA_URI
from .Utils import Tool

tool = Tool()


class WeiboHotPipeline:

    def open_spider(self, spider):

        self.hot_line = "index,mid,word,label_name,raw_hot,category,onboard_time\n"

        data_dir = os.path.join(DATA_URI)
        file_path = data_dir + '/hot.csv'
        #判断文件夹存放的位置是否存在,不存在则新建文件夹
        if os.path.isfile(file_path):
            self.data_file = open(file_path, 'a', encoding='utf-8')
        else:
            if not os.path.exists(data_dir):
                os.makedirs(data_dir)
            self.data_file = open(file_path, 'a', encoding='utf-8')
            self.data_file.write(self.hot_line)

    def close_spider(self, spider):  # 在关闭一个spider的时候自动运行
        self.data_file.close()

    def process_item(self, item, spider):
        try:

            hot_line = '{},{},{},{},{},{},{}\n'.format(
                item.get('index', ''),
                item.get('mid', ''),
                item.get('word', ''),
                item.get('label_name', ''),
                item.get('raw_hot', ''),
                tool.translate_chars(item.get('category', '')),
                tool.get_format_time(item.get('onboard_time', '')),
            )
            self.data_file.write(hot_line)
        except BaseException as e:
            print("hot错误在这里>>>>>>>>>>>>>", e, "<<<<<<<<<<<<<错误在这里")
        return item

三.注意事项

settings.py配置项修改

python 复制代码
# Obey robots.txt rules
ROBOTSTXT_OBEY = False # 关闭,否则根据weibo的爬虫策略爬虫无法获取数据

如果

四.运行过程

五.项目说明文档

六.获取完整源码

爱学习的小伙伴,本次案例的完整源码,已上传微信公众号"一个努力奔跑的snail ",后台回复 热搜榜 即可获取。

相关推荐
statistican_ABin3 分钟前
中国婚姻与生育观念变迁分析报告
python
清水白石00813 分钟前
别被 `Process.start()` 骗了:Python multiprocessing 从面试题到线上生产的进程启动机制实战
开发语言·python
愚公移山填海39 分钟前
【无标题】
笔记·python·学习·pycharm
天远API1 小时前
零信任架构实战:基于天远公安三要素即时版构建自动化理赔合规网关
人工智能·python·架构·自动化
言乐61 小时前
Python加速器2视频网页加速器
前端·javascript·css·python·音视频
计算机源码社1 小时前
基于Hadoop+Spark的黄金市场历史数据特征分析与可视化大屏 基于K-Means聚类算法的黄金历史价格阶段划分研究
大数据·hadoop·python·数据挖掘·数据分析·spark·毕业设计
hhzz1 小时前
【OpenCV 入门到精通 05】核心操作与像素处理:ROI、运算与性能优化
人工智能·python·opencv·性能优化
2601_962078231 小时前
Python接口自动化框架:pytest
python·pytest·接口自动化·测试框架·restfulapi
Alphapeople1 小时前
路径规划算法的python实现
开发语言·python·算法
旋生万物1 小时前
圈道裂痕——用 Python 验证素数分布的螺旋生成论判据
开发语言·python·数论·素数·cci·可计算数学