Giter Site home page Giter Site logo

kaidiguo / keyword_based_sina_weibo_crawler Goto Github PK

View Code? Open in Web Editor NEW
97.0 4.0 32.0 49 KB

A web crawler for Sina, search and retrieve microblogs that contain certain keywords 一个简单的python爬虫实践,爬取包含关键词的新浪微博

Python 100.00%
sina web-crawler python-json sina-weibo-crawler

keyword_based_sina_weibo_crawler's Introduction

keyword_based_Sina_weibo_crawler

A web crawler for Sina, search and retrieve microblogs that contain certain keywords 一个简单的python爬虫实践,爬取包含关键词的新浪微博

English


此项目主要功能是通过微博“搜索”页面,每天自动爬取所有包含自定list中词汇的微博原数据。

原为本人研究生论文【Spatial-temporal Analysis of International Connections Based on Textual Social Media Data】获取数据所用。

低速可控,简单粗暴,适合用来有针对性的搜集数据量不是很大的包含关键词的微博,每日可爬3-6万条

不过后来发现其实新浪有这个API,但是隐藏得很深,等我发现的时候这个爬虫已经写完了,抹泪

:(

さぁ、始めよう~


说明

  • 基于python 2(添加了3的版本)
  • 在 email_info.py中添加你自己的邮箱,密码,和接收邮箱
  • 在sina_crawler.py开头替换你自己的关键词列表
  • 日期格式转码和计算等方法都在function.py文件中
  • 后续的处理JSON,提取有用信息和我目标项目的本体部分请看用python处理微博JSON数据小例

项目介绍

  • 本项目没有UI,虽然简陋但是我写的第一个爬虫,贵在能跑
  • 本项目可以24小时不间断运行以获得更完整的微博爬取
  • 爬取微博启动和遇到bug的时候会发邮件给你
  • 获取的JSON数据写入txt,靠文件夹&文件名进行管理
  • 后续对获取到的JSON数据进行处理请查看 用python处理微博JSON数据范例

已获取的微博JSON数据按照request发起的日期分别存在相应的文件夹内部: WBTestdata>04-12.

每一页JSON包含十条微博数据(一般情况),将每次返回的JSON单调存在一个txt里,命名规则为“国家名”+“日期”+“页码”.

可以使用在线JSON结构化工具进行审查


项目背景

新浪微博各个客户端都提供“搜索”功能,可以得到包含关键词的微博,一般默认按照从新到老的发布顺序显示. 这里我们的目标页面是手机版的新浪微博 m.weibo.cn(因为结构简单,加载的微博数据直接以JSON文件返回,很容易获取) 比如,搜索关键词为德国时,页面显示如下:

image.png

打开开发者工具,选择network--XHR,然后你往下滚动页面直到有新的微博加载进来,你会发现下面那个链接:

点击它进行预览:

没错,这个就是我们的目标数据了--每当用户滚轮触底,就会通过此链接返回十条JSON格式的新微博. 我们看一下这个链接的格式:

https://m.weibo.cn/api/container/getIndex?type=all&queryVal=%E5%BE%B7%E5%9B%BD&featurecode=20000320&luicode=10000011&lfid=106003type%3D1&title=%E5%BE%B7%E5%9B%BD&containerid=100103type%3D1%26q%3D%E5%BE%B7%E5%9B%BD&page=2

解码一下URL,其实它就等于:

https://m.weibo.cn/api/container/getIndex?type=all&queryVal=德国& featurecode=20000320&luicode=10000011&lfid=106003type%3D1&title=德国&containerid=100103type%3D1%26q%3D德国&page= 1%26q%3D%E5% BE%B7%E5%9B%BD&page=1

关键信息一目了然,那就是queryVal=德国page=1根据这个规则我们就可以构建目标链接进行数据爬取了。

新浪的这个JSON数据就是所谓的一页(1 page),每次返回大概10条微博记录,但有时候也会少于10条,上图中card_group中有几个数字就是有几条记录.


代码结构

  • 先引入requestimport requests
  • 定义header伪装浏览器
# add header for the crawler
headers = {'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'}
  • 添加你想要检索的关键字的list
# Add in your search list!
search_list = ["所罗门群岛", "斯洛伐克", "贝宁",]
......
  • 将中文关键词转码成URL可以使用的,方便后面构造链接
# Create url encoded search list based on the word list you have gaven
urlencoded_search_list = url_encoding(search_list)
urls = create_url_list(urlencoded_search_list)
  • 写一个while: 1的循环执行不停歇的运转,每一次循环是执行当天的任务. 当天的任务我规定为: 从开始检索的page1开始持续往后搜索,并且获取每一page中最后一条微博的创建时间,如果这条微博创建于两天之内,那我们就继续往后获取页面,直到页面中最老一条的微博是两天前创建的,此时停止本词的搜索,进行下一个词语

这里需要这样操作的原因在于,新浪似乎并不会真的按照创建时间顺序依次返回所有的微博,两次同一瞬间的检索分别得到的10条微博中可能会有几条不一样的,而越老的微博,新浪返回的时间间隔越大 -- 举个例子,假设用户创造微博的速率是稳定的,在进行包含关键词AAA的检索时,page1中的10条微博都创建于5分钟内,互相间隔几秒,但当你查看page 100时,其中的10条微博则可能互相间隔几个小时 -- 这肯定是不科学的. 这就限制了我们在爬取微博的时候必须从新到老,并且限制不要爬太多页面(因为老数据过于稀松价值会降低),而且对某一个时段最好能重叠搜索,所以我创造了上面的规则,使每天的任务其实为【到现在为止前72小时内发布的微博】,这样时间上的重叠能更多的获取到更全的“老”微博。

  • 在while循环内
    1. 给自己发一封邮件提示程序开始了
    2. 根据日期创建每日数据的文件夹
    3. 对于每一个在list中的关键词从page=1开始往后检索,获取一页写下一页,同时获取每一页最后后一条微博的创建时间,判断是否停止搜索
    4. 完成所有关键词检索后,给自己发一封邮件提示今日任务完成了
    5. 打印一些数据信息,写每日记录,计算需要睡多久(保证每天在同一时刻开始任务以减少额外的未知误差)

注释非常详细,细节就请直接参考代码.

keyword_based_sina_weibo_crawler's People

Contributors

kaidiguo 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

Watchers

 avatar  avatar  avatar  avatar

keyword_based_sina_weibo_crawler's Issues

Extract_by_date.py doesn't work

Sorry to bother again...
The Extract_by_date.py stops at the middle of line 54 thisRequesttime = thisdata['cardlistInfo']['starttime'], print out in cmd only goes as far as <'cardlistInfo'>, then skips the entire json processing and jumps right to the exception where it says "No data in this file! Next!"
I tried millions ways please help...

请问这是什么问题

[WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。
failed to send mail

getting blank folders

Hi :) I am totally new on this please bear with me..
So I am trying to run this nice little crawler on python3 when I found that I am only getting blank folders named with the date, but nothing in it, and the process stuck at printing out the sentence "Log output finished". mylog.txt is nice and recorded, would you be so kind to tell me what is wrong? Thank you so much.
P.S. Also I got the exception message "Get this endtime fail", how should I change the JSON stucture?

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.