Git Product home page Git Product logo

weibocrawler's Introduction

WeiboCrawler

项目说明

项目介绍

新浪微博是国内主要的社交舆论平台,对社交媒体中的数据进行采集是舆论分析的方法之一。本项目无需cookie,可以连续爬取一个或多个新浪微博用户信息、用户微博及其微博评论转发。

实例

抓取用户信息 user 抓取用户微博 mblog 抓取微博转发 Repost 抓取微博评论 comment

使用方法

拉取项目

$ git clone https://github.com/XWang20/WeiboCrawler.git

安装依赖

本项目Python版本为Python3.8

$ cd WeiboCrawler
$ python -m pip install -r requirements.txt

安装数据库(可选)

默认使用MongoDB数据库,可在settings.py中修改URL和数据库名,默认为localhost、weibo。

运行程序

基本程序

在命令行中运行以下命令:

抓取用户信息

$ scrapy crawl user

抓取用户微博

$ scrapy crawl mblog

抓取微博转发

$ scrapy crawl repost

抓取微博评论

$ scrapy crawl comment

自定义选项

  1. 关键词检索,需要将./WeiboCrawler/spiders/mblog.py中第28行代码替换为urls = init_url_by_search(),并在init_url_by_search()中增加关键词列表。

  2. 采集id和时间范围等信息可根据自己实际需要重写./WeiboCrawler/spiders/*.py中的start_requests函数。

  3. 输出方式:支持输出到mongo数据库中,或输出json或csv文件。

如果输出json或csv文件,需要在命令后加入-o *.json-o *.csv,例如:

$ scrapy crawl user -o user.csv

如果输出到mongo数据库中,需要将./WeiboCrawler/settings.py中 mongo 数据库的部分取消注释:

ITEM_PIPELINES = {
    'WeiboCrawler.pipelines.MongoPipeline': 400,
}
MONGO_URI = 'localhost'
MONGO_DB = 'weibo'
  1. 添加账号cookie:可在settings.py中添加默认头,或在start_request函数中添加。

  2. 默认下载延迟为3,可在settings.py修改DOWNLOAD_DELAY。

  3. 默认会爬取二级评论,如果不需要可以在comment.py中注释以下代码:

if comment['total_number']:
secondary_url = 'https://m.weibo.cn/comments/hotFlowChild?cid=' + comment['idstr']
yield Request(secondary_url, callback=self.parse_secondary_comment, meta={"mblog_id": mblog_id})

无cookie版限制的说明

  • 单用户微博最多采集200页,每页最多10条 限制可以通过添加账号cookie解决。

设置多线程和代理ip

  • 多线程:(单ip池或单账号不建议采用多线程) 在settings.py文件中将以下代码取消注释:
# Configure maximum concurrent requests performed by Scrapy (default: 16)
CONCURRENT_REQUESTS = 100
# The download delay setting will honor only one of:
CONCURRENT_REQUESTS_PER_DOMAIN = 100
CONCURRENT_REQUESTS_PER_IP = 100
  • 代理ip池
  1. 填写middlewares.py中的fetch_proxy函数。
  2. settings.py文件中将以下代码取消注释:
# Enable or disable downloader middlewares
# See https://doc.scrapy.org/en/latest/topics/downloader-middleware.html
DOWNLOADER_MIDDLEWARES = {
    'WeiboCrawler.middlewares.IPProxyMiddleware': 100,
}
  1. settings.py文件中将DOWNLOAD_DELAY设置为0。
DOWNLOAD_DELAY = 0

字段说明

用户信息

  • _id: 用户ID
  • nick_name: 昵称
  • gender: 性别
  • brief_introduction: 简介
  • location: 所在地
  • mblogs_num: 微博数
  • follows_num: 关注数
  • fans_num: 粉丝数
  • vip_level: 会员等级
  • authentication: 认证,对于已认证用户该字段会显示认证信息
  • person_url: 首页链接

微博信息

  • _id: 微博id
  • bid: 微博bid
  • weibo_url: 微博URL
  • created_at: 微博发表时间
  • like_num: 点赞数
  • repost_num: 转发数
  • comment_num: 评论数
  • content: 微博内容
  • user_id: 发表该微博用户的id
  • tool: 发布微博的工具

转发信息

  • _id: 转发id
  • repost_user_id: 转发用户的id
  • content: 转发的内容
  • mblog_id: 转发的微博的id
  • created_at: 转发时间
  • source: 转发工具

评论信息

  • _id: 评论id
  • comment_user_id: 评论用户的id
  • content: 评论的内容
  • mblog_id: 评论的微博的id
  • created_at: 评论发表时间
  • like_num: 点赞数
  • root_comment_id: 根评论id,只有二级评论有该项
  • img_url: 图片地址
  • reply_comment_id: 评论的id,只有二级评论有该项

写在最后

本项目参考了dataabc/weibo-crawlernghuyong/WeiboSpider,感谢他们的开源。

欢迎为本项目贡献力量。欢迎大家提交PR、通过issue提建议(如新功能、改进方案等)、通过issue告知项目存在哪些bug、缺点等。

如有问题和交流,也欢迎联系我:[email protected]

weibocrawler's People

Contributors

xwang20 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

weibocrawler's Issues

关键词查找问题

有关关键词的配置还能生效吗,我按照说明将注释掉的代码恢复了,然后设置了keyword,但是还是无法生效

超棒的项目!可不可以再加一个微博url到mid的转化呀

因为weibo_id 字符串为 Hd1N2qpta(举个例子),项目中直接用的是转化出来的十进制数字。感觉自己再找个代码转化,有点麻烦。

(此外,可不可以出这样一个代码:话题下/关键词搜索会有很多微博,爬取这些所有微博的转发关系)
(我给您发的邮件就是这个意思啦,当时邮件里没有表述清楚,现在我的节点恢复了,能登陆github了,十分有幸遇到这么好的项目(o゚v゚)ノ)

求新增直接写入CSV

可不可以添加一个直接写入CSV的保存形式啊,小白选手对数据库比较吃力

评论爬取问题

image
工程师您好,在运行comment.py时爬不下来东西,很多都一条都没有,有一些能爬也只能爬个10%这样,感觉有点局限性,您有好的建议吗?

爬取数据的存储问题

1640168005(1)
您好我在使用本项目时代码正常运行,但每次爬取的数据无法及时储存,当程序结束后【user.csv】文件才有数据内容,请问大佬如何修改呀。

在转发爬取时遇到问题

作者您好!在我运行抓发爬取命令时遇到了如下问题:
2022-03-03 14:53:17 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://m.weibo.cn/api/statuses/repostTimeline?id=4742865873797609&page=1> (referer: None) 2022-03-03 14:53:17 [scrapy.core.scraper] ERROR: Spider error processing <GET https://m.weibo.cn/api/statuses/repostTimeline?id=4742865873797609&page=1> (referer: None) Traceback (most recent call last): File "C:\Users\17243\Desktop\WeiboCrawler\venv\lib\site-packages\scrapy\utils\defer.py", line 102, in iter_errback yield next(it) File "C:\Users\17243\Desktop\WeiboCrawler\venv\lib\site-packages\scrapy\spidermiddlewares\offsite.py", line 30, in process_spider_output for x in result: File "C:\Users\17243\Desktop\WeiboCrawler\venv\lib\site-packages\scrapy\spidermiddlewares\referer.py", line 339, in <genexpr> return (_set_referer(r) for r in result or ()) File "C:\Users\17243\Desktop\WeiboCrawler\venv\lib\site-packages\scrapy\spidermiddlewares\urllength.py", line 37, in <genexpr> return (r for r in result or () if _filter(r)) File "C:\Users\17243\Desktop\WeiboCrawler\venv\lib\site-packages\scrapy\spidermiddlewares\depth.py", line 58, in <genexpr> return (r for r in result or () if _filter(r)) File "C:\Users\17243\Desktop\WeiboCrawler\WeiboCrawler\spiders\repost.py", line 23, in parse all_page = js['data']['max'] KeyError: 'data'
初步分析是由于需要登录造成的,请教下您如何解决?

数据的具体时间获取问题

大佬你好,想在抓取评论时获取评论的具体时间,按照网上的代码在 comment.py 做了增添:

commentItem['created_at']= standardize_date(comment['created_at']).strftime('%Y-%m-%d %H:%M:%S')

部分结果显示 小时-分钟-秒,但都是00;还有部分结果还是只有年-月-日,不知道哪里出了问题,求解答🙏

comment 爬虫中存在的问题

工程师您好,我发现在爬微博评论时,(1)并不能够把所有微博评论都爬下来;(2)会返回很多空白行;(3)每次执行程序,返回的结果(也就是爬下来的数量)还不一致。我想可能是评论接口有一定几率返回空白,需要重复访问多次,才有结果。我不知道您有没有考虑这个问题呢?

转发爬取遇到的问题

请问我在爬取转发内容时发现每条微博能爬取的转发量是一个固定的数字,只有一百多条(微博转发的第十页),添加了cookie也没有变化,请问大佬应该如何完整地爬取转发内容?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.