如何爬取了知乎用户信息,并做了简单的分析
2017-04-03 15:39
288 查看
版权声明:本文为博主原创文章,转载请注明出处,博客地址为 http://blog.csdn.net/forezp ,谢谢。
目录(?)[-]
一使用的技术栈
二数据成果
三简单的可视化分析
四爬虫架构
五编码
六如何获取authorization
七可改进的地方
八关于ELK套件
九结语
分析工具: ELK套件
开发工具:pycharm
0 绿色代表的是男性 ^ . ^
1 代表的是女性
-1 性别不确定
可见知乎的用户男性颇多。
2.粉丝最多的top30
粉丝最多的前三十名:依次是张佳玮、李开复、黄继新等等,去知乎上查这些人,也差不多这个排名,说明爬取的数据具有一定的说服力。
3.写文章最多的top30
说明:
选择一个活跃的用户(比如李开复)的url作为入口url.并将已爬取的url存在set中。
抓取内容,并解析该用户的关注的用户的列表url,添加这些url到另一个set中,并用已爬取的url作为过滤。
解析该用户的个人信息,并存取到本地磁盘。
logstash取实时的获取本地磁盘的用户数据,并给elsticsearch
kibana和elasticsearch配合,将数据转换成用户友好的可视化图形。
2
3
4
5
6
7
8
9
10
11
12
13
14
15
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
解析内容:
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
存本地文件:
2
3
4
5
1
2
3
4
5
代码说明:
* 需要修改获取requests请求头的authorization。
* 需要修改你的文件存储路径。
源码下载:点击这里,记得star哦!
打开chorme,打开https://www.zhihu.com/,
登陆,首页随便找个用户,进入他的个人主页,F12(或鼠标右键,点检查)
点击关注,刷新页面,见图:
存储url的时候我才用的set(),并且采用缓存策略,最多只存2000个url,防止内存不够,其实可以存在redis中。
存储爬取后的用户我说采取的是本地文件的方式,更好的方式应该是存在mongodb中。
对爬取的用户应该有一个信息的过滤,比如用户的粉丝数需要大与100或者参与话题数大于10等才存储。防止抓取了过多的僵尸用户。
另外logstash的配置文件如下:
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
目录(?)[-]
一使用的技术栈
二数据成果
三简单的可视化分析
四爬虫架构
五编码
六如何获取authorization
七可改进的地方
八关于ELK套件
九结语
文章出处http://blog.csdn.net/liuyuehui110/article/details/68961006#6729808
一、使用的技术栈:
爬虫:python27 +requests+json+bs4+time分析工具: ELK套件
开发工具:pycharm
二、数据成果
三、简单的可视化分析
1.性别分布0 绿色代表的是男性 ^ . ^
1 代表的是女性
-1 性别不确定
可见知乎的用户男性颇多。
2.粉丝最多的top30
粉丝最多的前三十名:依次是张佳玮、李开复、黄继新等等,去知乎上查这些人,也差不多这个排名,说明爬取的数据具有一定的说服力。
3.写文章最多的top30
四、爬虫架构
爬虫架构图如下:说明:
选择一个活跃的用户(比如李开复)的url作为入口url.并将已爬取的url存在set中。
抓取内容,并解析该用户的关注的用户的列表url,添加这些url到另一个set中,并用已爬取的url作为过滤。
解析该用户的个人信息,并存取到本地磁盘。
logstash取实时的获取本地磁盘的用户数据,并给elsticsearch
kibana和elasticsearch配合,将数据转换成用户友好的可视化图形。
五.编码
爬取一个url:def download(url): if url is None: return None try: response = requests.get(url, headers={ 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36', 'authorization': 'your authorization ' }) print (response.content) if (response.status_code == 200): return response.content return None except: return None1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
解析内容:
def parse(response): try: print (response) json_body = json.loads(response); json_data = json_body['data'] for item in json_data: if (not old_url_tokens.__contains__(item['url_token'])): if(new_url_tokens.__len__()<2000): new_url_tokens.add(item['url_token']) if (not saved_users_set.__contains__(item['url_token'])): jj=json.dumps(item) save(item['url_token'],jj ) saved_users_set.add(item['url_token']) if (not json_body['paging']['is_end']): next_url = json_body['paging']['next'] response2 = download(next_url) parse(response2) except: print ('parse fail')1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
存本地文件:
def save(url_token, strs): f = file("\\Users\\forezp\\Downloads\\zhihu\\user_" + url_token + ".txt", "w+") f.writelines(strs) f.close()1
2
3
4
5
1
2
3
4
5
代码说明:
* 需要修改获取requests请求头的authorization。
* 需要修改你的文件存储路径。
源码下载:点击这里,记得star哦!
六.如何获取authorization
打开chorme,打开https://www.zhihu.com/,登陆,首页随便找个用户,进入他的个人主页,F12(或鼠标右键,点检查)
点击关注,刷新页面,见图:
七、可改进的地方
可增加线程池,提高爬虫效率存储url的时候我才用的set(),并且采用缓存策略,最多只存2000个url,防止内存不够,其实可以存在redis中。
存储爬取后的用户我说采取的是本地文件的方式,更好的方式应该是存在mongodb中。
对爬取的用户应该有一个信息的过滤,比如用户的粉丝数需要大与100或者参与话题数大于10等才存储。防止抓取了过多的僵尸用户。
八.关于ELK套件
关于elk的套件安装就不讨论了,具体见官网就行了。网站:https://www.elastic.co/另外logstash的配置文件如下:
input { # For detail config for log4j as input, # See: https://www.elastic.co/guide/en/logstash/current/plugins-inputs-log4j.html file { path => "/Users/forezp/Downloads/zhihu/*" } } filter { #Only matched data are send to output. } output { # For detail config for elasticsearch as output, # See: https://www.elastic.co/guide/en/logstash/current/plugins-outputs-elasticsearch.html elasticsearch { action => "index" #The operation on ES hosts => "localhost:9200" #ElasticSearch host, can be array. index => "zhihu" #The index to write data to. } }1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
九、结语
从爬取的用户数据可分析的地方很多,比如地域、学历、年龄等等,我就不一一列举了。另外,我觉得爬虫是一件非常有意思的事情,在这个内容消费升级的年代,如何在广阔的互联网的数据海洋中挖掘有价值的数据,是一件值得思考和需不断践行的事情。最后,本文仅用作交流学习。如果知乎告知我侵权,我会立刻删除本文。相关文章推荐
- 如何爬取了知乎用户信息,并做了简单的分析
- 爬取知乎60万用户信息之后的简单分析
- 爬取知乎60万用户信息之后的简单分析
- 简单Python3爬虫程序(5)进阶:知乎网的登录与用户相关信息爬取
- [python和大数据-1]利用爬虫登录知乎进行BFS搜索抓取用户信息本地mysql分析【PART1】
- 简单的python3.5爬取知乎用户基本信息
- VC如何保存和读取用户信息?
- 简单的获取用户信息
- 在Exchange Server 2003的ESM中如何查看用户邮箱信息?
- 在Exchange Server 2003的ESM中如何查看用户邮箱信息?
- 如何通过销售发票和日记帐事务信息进行销售交货与退货信息分析
- python下如何让web元素的生成更简单的分析
- 如何让用户再次访问我的网站时不需再提交相关信息?
- 用户登录安全性的简单实例分析(Cookie、加密)
- 如何管理域用户信息,包括(增删改查等功能)
- 得到一个wma或mp3文件,如何用最简单的方法得到它的长度信息呢?
- python下如何让web元素的生成更简单的分析
- ASP.NET是如何依据用户POST的信息去执行程序?
- 如何实现登陆后,用户信息使用于多个站点?
- 大型机学习之具体技术之-JCL练习(六)-如何分析作业的输出信息