思路
两个程序。
一个程序负责爬取用户关注和粉丝列表, 并把用户名存入set
另一个程序负责根据用户名获取详细信息,存入hash
维护 两个列表 1.已爬用户 2.未爬用户
第一个程序的流程:
当未爬set不为空时:执行如下操作:每次从未爬取列表中取出一个用户名 根据用户名,获取他的关注与粉丝并遍历,如果用户既没在已爬用户,也没在未爬用户 加入未爬用户列表。
第二个程序的流程
每次从未爬set中取出一个用户名,调用接口获取详细信息并存入redishash中
接口获取
打开某个人的个人主页,按F12

选择XHR 然后点击图1的关注我的人,和我关注的人,在图2就可以看到header和cookie。
获取用户列表Api:
https://www.zhihu.com/api/v4/...{username}/{type}?include=data[*].answer_count,articles_count,gender,follower_count,is_followed,is_following,badge[?(type=best_answerer)].topics&offset=20&limit=20
username 是用户名,
type 是类型, 有两种可选 [followers,followees], followers是粉丝 followees是关注
改变offset的值可以获取分页
获取个人信息Api:
https://www.zhihu.com/people/...
user_token是用户名
这个api中 返回的是html页面,在html页面中有一个scirpt标签里面的内容是json格式的用户信息,这就很方便了,直接获取键值对 对应的属性就ok。
发送请求
F12查看接口,把header和cookie放进requests.get()方法中。
剩下的就很简单了,不赘述了。
GitHub:https://github.com/MasakiOvO/zhihu-userinfo-spider

网友评论