共计 1655 个字符,预计需要花费 5 分钟才能阅读完成。
提醒:本文最后更新于 2019-06-14 16:18,文中所关联的信息可能已发生改变,请知悉!
全文简介
对于近日让人气愤的长春疫苗事件,京东集团刘强东在微头条发表看法,一时间获取广大粉丝热评,评论人数也是呈现指数级的增长,那么大家对这件事的态度都是什么呢?我们用 Python 来看看大家都在说些什么。
涉及的 Python 爬虫知识:
- APP 抓包
- Scrapy 框架的使用
- 词频统计
- 词云可视化
任务分析
我们想要获得的数据是今日头条里面的微头条,搜索刘强东用户即可找到该微头条信息。然后我们采用手机抓包的方式来发现并获取数据。接下来再用 Python 中的爬虫框架 Scrapy 来抓取数据,并且将数据保存在数据库或者本地文件即可。最后是 NLP(自然语言处理)的简单应用,主要包含词云的展示。
今日头条手机 APP 抓包
对于 APP 数据的抓取首先要用到 APP 抓包工具,这种工具有那么几个,我用的是 Fiddler4 来实现的抓包。需要将你的 WIFI 和你的网络在相同 IP 下。具体的配置方法,大家可以自行进行百度。
我在这里提供几个参考的文章,供大家阅读。
Fiddler 实现手机抓包 –
如何在 Android 手机上实现抓包?
一图胜千言 ,抓包结果图如下图所示:

将得到的网址 url 复制到浏览器(火狐浏览器)里面会变成下图这个样子:

返回的是 JSON 文件类型的数据,很方便提取数据。另外我们精简一下网址,去掉一些无关紧要的查询字符,得到的精简网址如下:
一行太长,进行换行以后的结果:

Scrapy 抓取 APP 数据
要想实现快速并且简单的抓取数据,爬虫框架要熟练掌握!而 Scrapy 框架又是一款功能强大的框架,该模块是爬虫必须掌握的模块!基本的用法大家可以参考 Scrapy 官网提供的教程,这个应该也是目前最好的教程了。
根据上一小节抓取到的网址结构,我们可以发现:
偏移量会随着每次的改变来翻页,所以我们只需在 Scrapy 里面的 start_urls 这么写就好(大概就是爬取了 30 万条):





数据爬完以后只能获取 12 万数据,到最后数据就是重复了(不再返回数据)!

词云可视化
接下来对得到的数据进行词云可视化的展示:
宝宝云图:






