今天用scrapy框架爬取一下所有知乎用户的信息。道理很简单,找一个知乎大V(就是粉丝和关注量都很多的那种),找到他的粉丝和他关注的人的信息,然后分别再找这些人的粉丝和关注的人的信息,层层递进,这样下来,只要有关注的人或者有粉丝的账号,几乎都能被爬下来。话不多说,进入正题。

1、首先按照上篇博客的介绍,先建立项目,然后建一个spider文件,scrapy  genspider  zhihu  www.zhihu.com.

进入settings.py,修改内容 ROBOTSTXT_OBEY = False,意思是知乎网页中禁止爬取的内容也可以爬的到。

再添加一个User_agent,因为知乎是通过浏览器识别的,否则知乎会禁止爬取

DEFAULT_REQUEST_HEADERS = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language': 'en',
'User-agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'
}

2、进入知乎网页页面,搜索一个大V,这里我用的是vczh,这个账号的关注量和粉丝都特别多。

  改写zhihu.py的内容如下:

from scrapy import Request,Spider

class ZhihuSpider(Spider):
name = 'zhihu'
allowed_domains = ['www.zhihu.com']
start_urls = ['http://www.zhihu.com/'] def start_requests(self): # zhihu.py 会先调用 start_requests 函数
url = 'https://www.zhihu.com/api/v4/members/ji-he-61-7?include=allow_message%2Cis_followed%2Cis_following%2Cis_org%2Cis_blocking%2Cemployments%2Canswer_count%2Cfollower_count%2Carticles_count%2Cgender%2Cbadge%5B%3F(type%3Dbest_answerer)%5D.topics'
yield Request(url,callback=self.parse) def parse(self, response):
print(response.text)

随便找一个vczh关注的人,把他的Request URL拿过来请求一下,发现正常输出了结果,说明是能够获取 vczh 自己的信息的。

将URL换成 followees(vczh关注的人) 的Request URL,同样输出了正确的结果,说明 vczh 关注的人的信息也能正常输出。

经过观察发现,每个用户详细信息的 URL 只有 url_token 不同,因此分别构造 user_url 和 follows_url:

class ZhihuSpider(Spider):
name = 'zhihu'
allowed_domains = ['www.zhihu.com']
start_urls = ['http://www.zhihu.com/'] start_user = 'excited-vczh' user_url = 'https://www.zhihu.com/api/v4/members/{user}?include={include}'
user_query = 'allow_message,is_followed,is_following,is_org,is_blocking,employments,answer_count,follower_count,articles_count,gender,badge[?(type=best_answerer)].topics' follows_url = 'https://www.zhihu.com/api/v4/members/{user}/followees?include={include}&offset={offset}&limit={limit}'
follows_query = 'data[*].answer_count,articles_count,gender,follower_count,is_followed,is_following,badge[?(type=best_answerer)].topics' def start_requests(self):
yield Request(self.user_url.format(user=self.start_user, include=self.user_query),callback=self.parse_user)
yield Request(self.follows_url.format(user=self.start_user, include=self.follows_query, offset=0, limit=20),callback=self.parse_follows) def parse_user(self, response):
result = json.loads(response.text)
item = Zhihu3Item()
for field in item.fields:
if field in result.keys():
item[field] = result.get(field)
yield item def parse_follows(self, response):
results = json.loads(response.text)
if 'data' in results.keys():
for result in results.get('data'):
yield Request(self.user_url.format(user=result.get('url_token'), include=self.user_query),
self.parse_user) if 'paging' in results.keys() and results.get('paging').get('is_end') == False: # 如果当前页不是最后一页
next_page_str = results.get('paging').get('next')
next_page = next_page_str.replace('https://www.zhihu.com/', 'https://www.zhihu.com/api/v4/')
# next_page = next_page_str[0:22] + 'api/v4/' + next_page_str[22:len(next_page_str)] # 这种写法也行
yield Request(next_page, self.parse_follows)

结果正确输出了 vczh 和 他所关注的用户的详细信息。

3、接下来,把 vczh 关注的用户 所 关注的用户的信息输出来:

在  def parse_user(self, response): 函数后添加一句

yield Request(self.follows_url.format(user=result.get('url_token'), include=self.follows_query, offset=0, limit=20),self.parse_follows) 就好了。

4、除了获取用户关注的人以外,还要获取用户的粉丝信息。经观察,发现粉丝信息的 URL 与关注的人的 URL 类似,改写以后的最终版本如下:

class ZhihuSpider(Spider):
name = 'zhihu'
allowed_domains = ['www.zhihu.com']
start_urls = ['http://www.zhihu.com/'] start_user = 'excited-vczh' user_url = 'https://www.zhihu.com/api/v4/members/{user}?include={include}'
user_query = 'allow_message,is_followed,is_following,is_org,is_blocking,employments,answer_count,follower_count,articles_count,gender,badge[?(type=best_answerer)].topics' follows_url = 'https://www.zhihu.com/api/v4/members/{user}/followees?include={include}&offset={offset}&limit={limit}'
follows_query = 'data[*].answer_count,articles_count,gender,follower_count,is_followed,is_following,badge[?(type=best_answerer)].topics' followers_url = 'https://www.zhihu.com/api/v4/members/{user}/followers?include={include}&offset={offset}&limit={limit}'
followers_query = 'data[*].answer_count,articles_count,gender,follower_count,is_followed,is_following,badge[?(type=best_answerer)].topics' def start_requests(self):
yield Request(self.user_url.format(user=self.start_user, include=self.user_query),callback=self.parse_user)
yield Request(self.follows_url.format(user=self.start_user, include=self.follows_query, offset=0, limit=20),callback=self.parse_follows)
yield Request(self.followers_url.format(user=self.start_user, include=self.followers_query, offset=0, limit=20),callback=self.parse_followers) def parse_user(self, response):
result = json.loads(response.text)
item = Zhihu3Item()
for field in item.fields:
if field in result.keys():
item[field] = result.get(field)
yield item yield Request(self.follows_url.format(user=result.get('url_token'), include=self.follows_query, offset=0, limit=20),self.parse_follows)
yield Request(self.followers_url.format(user=result.get('url_token'), include=self.followers_query, offset=0, limit=20),self.parse_followers) def parse_follows(self, response):
results = json.loads(response.text)
if 'data' in results.keys():
for result in results.get('data'):
yield Request(self.user_url.format(user=result.get('url_token'), include=self.user_query),
self.parse_user) if 'paging' in results.keys() and results.get('paging').get('is_end') == False: # 如果当前页不是最后一页
next_page_str = results.get('paging').get('next')
next_page = next_page_str.replace('https://www.zhihu.com/', 'https://www.zhihu.com/api/v4/')
# next_page = next_page_str[0:22] + 'api/v4/' + next_page_str[22:len(next_page_str)] # 这种写法也行
yield Request(next_page, self.parse_follows) def parse_followers(self, response):
results = json.loads(response.text)
if 'data' in results.keys():
for result in results.get('data'):
yield Request(self.user_url.format(user=result.get('url_token'), include=self.user_query),
self.parse_user) if 'paging' in results.keys() and results.get('paging').get('is_end') == False: # 如果当前页不是最后一页
next_page_str = results.get('paging').get('next')
next_page = next_page_str.replace('https://www.zhihu.com/', 'https://www.zhihu.com/api/v4/')
# next_page = next_page_str[0:22] + 'api/v4/' + next_page_str[22:len(next_page_str)] # 这种写法也行
yield Request(next_page, self.parse_followers)

5、将输出的内容保存到MongoDb中

跟上一篇博文类似,在pipeline.py中写入一下内容,直接上代码:

import pymongo

class MongoPipeline(object):
def __init__(self, mongo_uri, mongo_db):
self.mongo_uri = mongo_uri
self.mongo_db = mongo_db @classmethod
def from_crawler(cls, crawler):
return cls(
mongo_uri=crawler.settings.get('MONGO_URI'),
mongo_db=crawler.settings.get('MONGO_DB')
) def open_spider(self, spider):
self.client = pymongo.MongoClient(self.mongo_uri)
self.db = self.client[self.mongo_db] def process_item(self, item, spider):
self.db['information'].update({'url_token':item['url_token']},{'$set':item},True)
return item # 根据'url_token'去重,True 表示如果重复就执行刷新操作,如果不重复就执行插入操作 def close_spider(self, spider):
self.client.close()

在settings.py中写入以下代码:

ITEM_PIPELINES = {
'zhihu_3.pipelines.MongoPipeline': 300
}
MONGO_URI='localhost'
MONGO_DB = 'zhihu_3'

运行即可,成功保存到MongoDB中

Srapy 爬取知乎用户信息的更多相关文章

  1. 基于webmagic的爬虫小应用--爬取知乎用户信息

    听到“爬虫”,是不是第一时间想到Python/php ? 多少想玩爬虫的Java学习者就因为语言不通而止步.Java是真的不能做爬虫吗? 当然不是. 只不过python的3行代码能解决的问题,而Jav ...

  2. 利用 Scrapy 爬取知乎用户信息

    思路:通过获取知乎某个大V的关注列表和被关注列表,查看该大V和其关注用户和被关注用户的详细信息,然后通过层层递归调用,实现获取关注用户和被关注用户的关注列表和被关注列表,最终实现获取大量用户信息. 一 ...

  3. 爬虫(十六):scrapy爬取知乎用户信息

    一:爬取思路 首先我们应该找到一个账号,这个账号被关注的人和关注的人都相对比较多的,就是下图中金字塔顶端的人,然后通过爬取这个账号的信息后,再爬取他关注的人和被关注的人的账号信息,然后爬取被关注人的账 ...

  4. 爬虫实战--利用Scrapy爬取知乎用户信息

    思路: 主要逻辑图:

  5. python3编写网络爬虫22-爬取知乎用户信息

    思路 选定起始人 选一个关注数或者粉丝数多的大V作为爬虫起始点 获取粉丝和关注列表 通过知乎接口获得该大V的粉丝列表和关注列表 获取列表用户信息 获取列表每个用户的详细信息 获取每个用户的粉丝和关注 ...

  6. [Python爬虫] Selenium爬取新浪微博客户端用户信息、热点话题及评论 (上)

    转载自:http://blog.csdn.net/eastmount/article/details/51231852 一. 文章介绍 源码下载地址:http://download.csdn.net/ ...

  7. 使用python scrapy爬取知乎提问信息

    前文介绍了python的scrapy爬虫框架和登录知乎的方法. 这里介绍如何爬取知乎的问题信息,并保存到mysql数据库中. 首先,看一下我要爬取哪些内容: 如下图所示,我要爬取一个问题的6个信息: ...

  8. 第二个爬虫之爬取知乎用户回答和文章并将所有内容保存到txt文件中

    自从这两天开始学爬虫,就一直想做个爬虫爬知乎.于是就开始动手了. 知乎用户动态采取的是动态加载的方式,也就是先加载一部分的动态,要一直滑道底才会加载另一部分的动态.要爬取全部的动态,就得先获取全部的u ...

  9. 【Python项目】爬取新浪微博个人用户信息页

    微博用户信息爬虫 项目链接:https://github.com/RealIvyWong/WeiboCrawler/tree/master/WeiboUserInfoCrawler 1 实现功能 这个 ...

随机推荐

  1. Rocket - util - PrefixSum

    https://mp.weixin.qq.com/s/G2vLP-ncoJzSOgxGGEJkfA   简单介绍PrefixSum的实现.   ​​   1. 基本介绍   ​​ 把一个序列从前向后逐 ...

  2. background-color的覆盖范围

    1. 一般div的background-color覆盖范围 到 border,margin的颜色由外层元素决定 2. body的background-color覆盖范围 到 margin,但 当htm ...

  3. Java实现 LeetCode 187 重复的DNA序列

    187. 重复的DNA序列 所有 DNA 都由一系列缩写为 A,C,G 和 T 的核苷酸组成,例如:"ACGAATTCCG".在研究 DNA 时,识别 DNA 中的重复序列有时会对 ...

  4. Java实现 LeetCode 8 字符串转换整数(atoi)

    8. 字符串转换整数 (atoi) 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我们寻找到的第一个非 ...

  5. Java实现蓝桥杯快乐数

    [问题描述] 判断一个正整数是否是快乐数字? 如果一个数字能够通过有限次快乐变换成为1,则是快乐数字. 快乐变换是对一个数字的每一位的平方数求和. 例如: 对于68 68 => 62+82= 1 ...

  6. java实现第三届蓝桥杯机器人行走

    机器人行走 [编程题](满分18分) 某少年宫引进了一批机器人小车.可以接受预先输入的指令,按指令行动.小车的基本动作很简单,只有3种:左转(记为L),右转(记为R),向前走若干厘米(直接记数字). ...

  7. java实现第四届蓝桥杯公式求值

    公式求值 输入n, m, k,输出图1所示的公式的值.其中C_n^m是组合数,表示在n个人的集合中选出m个人组成一个集合的方案数.组合数的计算公式如图2所示. 输入的第一行包含一个整数n:第二行包含一 ...

  8. Babel 7 安装与配置

    Babel:帮我们把高级的语法(ES6)转为低级的语法 /*    Babel 7.x版本  安装如下 (cnpm i @babel/cli -D)                     //Bab ...

  9. sqlite使用dbexpress时数据库不存在自动建立数据库

    在发布使用delphi dbexpress编写的基于SQLITE的程序时,需要在运行时判断某个数据库是否存在,如果不存在,则自动建立. 方法有2,其中之一是判断数据库文件是否存在,如果不存在,则创建一 ...

  10. 性能调优必备利器之 JMH

    if 快还是 switch 快?HashMap 的初始化 size 要不要指定,指定之后性能可以提高多少?各种序列化方法哪个耗时更短? 无论出自何种原因需要进行性能评估,量化指标总是必要的. 在大部分 ...