Python爬虫抓取知乎所有用户信息

今天用递归写了个抓取知乎所有用户信息的爬虫,源代码放在了github上,有兴趣的同学可以上去下载一下看看,这里介绍一下代码逻辑以及分页分析,首先看网页,这里本人随便选了一个大V作为入口,然后点开他的关注列表,如图

640?wx_fmt=jpeg&tp=webp&wxfrom=5&wx_lazy

注意,本人爬虫的全称都是处于非登录状态的。这里的粉丝列表以及关注者列表都是后台ajax请求得到的数据(没有听过ajax的童鞋别慌,ajax请求跟普通浏览器的请求没有区别,它主要就是在我们 浏览网页时候偷偷给服务器发送的请求,就是为了节省流量以及减少请求数,不然每次看点新数据都全部刷新网页,服务器压力很大的,所以有了这玩意),然后我们找到粉丝列表以及关注者列表的URL,这个很简单,在chrome浏览器下面点击一下页数切换就可以找到,如图

640?wx_fmt=jpeg&tp=webp&wxfrom=5&wx_lazy

找到关注者以及粉丝的URL就好办理,下面看一看这些数据,这里以粉丝的数据举例,如图,是一段json

640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=

这里找到了粉丝的数据,不过这里不是用户的详细信息,只有部分数据,不过他提供了一个token_url,我们就可以获取这个ID访问用户的详细信息了,我们看看每个用户的详细信息怎么提取。这里楼主发现,在观看粉丝或者关注列表的时候,网页是会自动触发该用户详细信息的请求,如图

640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=

这次获得的是用户详细信息查询的URL,这里看一看这个详细信息的URL,如图

640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=

上面介绍了网页的基础分析,下面说一下代码的思路,这次爬虫用到了递归,本次用的scrapy抓取以及mogodb数据库存储的。
首先本人是用了一个大V作为爬虫第一个网页,然后分三步,第一步是爬了该大V的详细信息然后存入数据库,第二步是爬取了该大V的粉丝,第三是爬取了该大V 的关注者(其实就是爬取粉丝或者关注者的token_url),完成之后,利用爬取的粉丝以及关注者的数据构造他们每个人详细信息的url,然后挖取详细 信息存入数据库。到这里递归第一步算是完成了,然后爬虫会从每一个粉丝和关注者入手,分别爬取他们的粉丝以及关注者的详细数据,不断递归
在代码里面还有加入了一些自动翻页的功能,有兴趣可以看看。下面是我们item里面定义要抓取的数据:
import scrapyclass ZhihuUserItem(scrapy.Item):
 # define the fields for your item here like:
 # name = scrapy.Field()
 answer_count = scrapy.Field()

#回答数量
 articles_count = scrapy.Field()

#写过的文章数
 follower_count = scrapy.Field()

#粉丝数量
 following_count = scrapy.Field()

#关注了多少人
 educations=scrapy.Field()

#教育背景
 description = scrapy.Field()

#个人描述
 locations = scrapy.Field()

#所在地
 url_token =scrapy.Field()

#知乎给予的每个人用户主页唯一的ID
 name=scrapy.Field()

#用户昵称
 employments = scrapy.Field()

#工作信息
 business=scrapy.Field()

#一些工作或者商业信息的合集
 user_type =scrapy.Field()

#用户类型,可以是个人,也可以是团体等等
 headline =scrapy.Field()

#个人主页的标签
 voteup_count = scrapy.Field()

#获得的赞数
 thanked_count=scrapy.Field()

#获得的感谢数
 favorited_count = scrapy.Field()

#被收藏次数
 avatar_url = scrapy.Field()

#头像URl

代码一共不足80行,运行了一分钟就抓了知乎一千多个用户的信息,这里上张结果图

640?wx_fmt=jpeg&tp=webp&wxfrom=5&wx_lazy
最近忙完别的事了,终于可以天天写爬虫了,不知道大家这篇有什么问题不,可以随便向我提
最后提一提,爬取一定要伪装好headers,里面有些东西服务器每次都会检查。

原文发布时间为:2017-04-09
本文作者:蜗牛仔
本文来自云栖社区合作伙伴“ Python中文社区”,了解相关信息可以关注“ Python中文社区”微信公众号
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用 Python 的 Requests 和 BeautifulSoup 库来爬取用户信息。首先需要登录乎获取 cookie,然后通过模拟登录获取到用户的个人主页,再使用 BeautifulSoup 解析页面获取用户信息。 以下是示例代码: ```python import requests from bs4 import BeautifulSoup # 登录乎并获取 cookie session = requests.Session() headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'} login_url = 'https://www.zhihu.com/signin' response = session.get(login_url, headers=headers) soup = BeautifulSoup(response.text, 'html.parser') _xsrf = soup.find('input', attrs={'name': '_xsrf'})['value'] captcha_url = soup.find('img', attrs={'class': 'Captcha-englishImg'})['src'] # 模拟登录获取用户信息 login_data = { '_xsrf': _xsrf, 'email': 'your_account', 'password': 'your_password', 'captcha': input('请输入验证码' + captcha_url), 'remember_me': 'true' } session.post(login_url, headers=headers, data=login_data) user_url = 'https://www.zhihu.com/people/username' response = session.get(user_url, headers=headers) soup = BeautifulSoup(response.text, 'html.parser') # 解析页面获取用户信息 name = soup.find('span', attrs={'class': 'ProfileHeader-name'}).text headline = soup.find('span', attrs={'class': 'RichText ztext ProfileHeader-headline'}).text description = soup.find('div', attrs={'class': 'ProfileHeader-infoItem ProfileHeader-description'}).find('span', attrs={'class': 'RichText ztext'}).text.strip() location = soup.find('div', attrs={'class': 'ProfileHeader-infoItem ProfileHeader-location'}).find('span', attrs={'class': 'ProfileHeader-detailValue'}).text.strip() business = soup.find('div', attrs={'class': 'ProfileHeader-infoItem ProfileHeader-business'}).find('span', attrs={'class': 'ProfileHeader-detailValue'}).text.strip() employment = soup.find('div', attrs={'class': 'ProfileHeader-infoItem ProfileHeader-employment'}).find('span', attrs={'class': 'ProfileHeader-detailValue'}).text.strip() position = soup.find('div', attrs={'class': 'ProfileHeader-infoItem ProfileHeader-position'}).find('span', attrs={'class': 'ProfileHeader-detailValue'}).text.strip() education = soup.find('div', attrs={'class': 'ProfileHeader-infoItem ProfileHeader-education'}).find('span', attrs={'class': 'ProfileHeader-detailValue'}).text.strip() major = soup.find('div', attrs={'class': 'ProfileHeader-infoItem ProfileHeader-major'}).find('span', attrs={'class': 'ProfileHeader-detailValue'}).text.strip() ``` 以上代码中,需要替换 `your_account` 和 `your_password` 为你的乎登录账号和密码,并将 `username` 替换为你要爬取的用户用户名。另外,为了防止被乎反爬虫机制检测到,最好加上一些随机的等待时间和 User-Agent 等信息

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值