# -*- coding: utf-8 -*-
# scrapy爬取全部知乎用户信息
# 1:是否遵守robbots_txt协议改为False
# 2: 加入爬取所需的headers: user-agent,authorazation
# 3:确定爬取任务:即想要得到的用户信息
# 4: 爬取思路解析
# 整体思路:从起始大v开始,获得其关注列表和粉丝列表;解析列表,可以得到每一个用户的详细信息地址,组成每一个用户的url;
# 从用户的url开始,解析用户详细信息,取到详细信息。同时又可以解析到每一个用户的关注列表和粉丝列表,循环请求。
# 分步骤如下:
# 4-1:找到起始大v,请求其页面,循环翻页获取其全部的关注列表,粉丝列表
# 4-2:列表步骤:解析关注列表,粉丝列表,从所有列表中取得用户的url_token,组成用户url,执行用户步骤4-3
# 4-3:用户步骤:解析用户url,该步骤可以获得1.该用户详细信息 2.该用户全部的关注列表与粉丝列表,返回列表步骤4-2
# 4-4:同步存储item到数据库mongodb,去重设计。
import json
import scrapy
from zhihu2.items import Zhihu2Item

class ZhihuuserSpider(scrapy.Spider):
    name = 'zhihuuser'
    allowed_domains = ['www.zhihu.com']
    start_urls = ['http://www.zhihu.com/']
    start_user = 'excited-vczh'
    # 一:对用户关注列表的请求构造
    # 用户关注列表 start_user为起始大v,followees_include为请求参数,limit为每页显示用户数,默认20,offset为页码参数,首页为0
    followees_url = 'https://www.zhihu.com/api/v4/members/{user}/followees?include={include}&offset={offset}&limit={limit}'
    followees_include = 'data[*].answer_count,articles_count,gender,follower_count,is_followed,is_following,badge[?(type=best_answerer)].topics'

    # 二:对用户粉丝列表的请求构造
    # 用户关注列表 start_user为起始大v,followees_include为请求参数,limit为每页显示用户数,默认20,offset为页码参数,首页为0
    followers_url = 'https://www.zhihu.com/api/v4/members/{user}/followers?include={include}&offset={offset}&limit={limit}'
    followers_include = 'data[*].answer_count,articles_count,gender,follower_count,is_followed,is_following,badge[?(type=best_answerer)].topics'

    # 三:对用户详细信息的请求构造
    user_url = 'https://www.zhihu.com/api/v4/members/{user}?include={include}'
    user_include = 'allow_message,is_followed,is_following,is_org,is_blocking,employments,answer_count,follower_count,articles_count,gender,badge[?(type=best_answerer)].topics'
    def start_requests(self):
        # 分别举列表url和用户url示例,以验证是否能够爬取
        # 关注列表url示例
        # 返回401是请求验证用户的身份,知乎的首页是要求验证用户的身份才能进入,所以需要在settings里面设置authorization
        # url='https://www.zhihu.com/api/v4/members/excited-vczh/followees?include=data%5B*%5D.answer_count%2Carticles_count%2Cgender%2Cfollower_count%2Cis_followed%2Cis_following%2Cbadge%5B%3F(type%3Dbest_answerer)%5D.topics&offset=60&limit=20'
        # 用户详细url示例
        # url='https://www.zhihu.com/api/v4/members/lanfengxing?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 scrapy.Request(url, callback=self.parse)
        # 构造用户关注列表的请求 主要用到format方法

        yield scrapy.Request(url=self.followees_url.format(user=self.start_user, include=self.followees_include, offset=0, limit=20), callback=self.parse_followees)
        # 构造用户粉丝列表的请求 主要用到format方法
        yield scrapy.Request(url=self.followers_url.format(user=self.start_user, include=self.followers_include, offset=0, limit=20),callback=self.parse_followers)
        # 对用户详细信息的请求构造
        yield scrapy.Request(url=self.user_url.format(user=self.start_user, include=self.user_include),callback=self.parse_user)
    # 解析关注列表
    def parse_followees(self, response):
        results = json.loads(response.text)
        if 'data' in results.keys():
            for result in results.get('data'):
                # 解析关注列表,得到所关注人的url_token,构造解析详细信息请求
                yield scrapy.Request(url=self.user_url.format(user=result.get('url_token'), include=self.user_include),callback=self.parse_user)
        # 构造翻页请求
        if 'paging' in results.keys() and results.get('paging').get('is_end')==False:
            next = results.get('paging').get('next')
            yield scrapy.Request(url=next, callback=self.parse_followees)

    # 解析粉丝列表
    def parse_followers(self, response):
        results = json.loads(response.text)
        if 'data' in results.keys():
            for result in results.get('data'):
                # 解析关注列表,得到所关注人的url_token,构造解析详细信息请求
                yield scrapy.Request(url=self.user_url.format(user=result.get('url_token'), include=self.user_include),
                                     callback=self.parse_user)
        # 构造翻页请求
        if 'paging' in results.keys() and results.get('paging').get('is_end') == False:
            next = results.get('paging').get('next')
            yield scrapy.Request(url=next, callback=self.parse_followers)

    # 解析用户详细信息,由于我们任务的目标是获取用户详细信息,因此在这一步要确定哪些信息是被使用,在items里面做相应设置
    def parse_user(self, response):
        item = Zhihu2Item()
        # 返回的response是json格式,因此需要解析json
        results = json.loads(response.text)
        # 遍历item数据结构的键名,item.field可以得到数据结构的所有键
        for field in item.fields:
            # 如果item的键名在网页里面,则遍历赋值
            if field in results.keys():
                item[field]=results.get(field)
        yield item

        # 提取用户的关注列表
        yield scrapy.Request(url=self.followees_url.format(user=results.get('url_token'),include = self.followees_include,offset=0, limit=20),callback=self.parse_followees)
        # 提取用户的粉丝列表
        yield scrapy.Request(url=self.followers_url.format(user=results.get('url_token'), include=self.followers_include, offset=0, limit=20),callback=self.parse_followers)
# -*- coding: utf-8 -*-

# Define here the models for your scraped items
#
# See documentation in:
# http://doc.scrapy.org/en/latest/topics/items.html

import scrapy

# 想要获取的用户信息设置
class Zhihu2Item(scrapy.Item):
    # define the fields for your item here like:
    # name = scrapy.Field()

    # 姓名 
    name = scrapy.Field()
    # 性别
    gender = scrapy.Field()
    # 职业
    employments = scrapy.Field()
    # 级别
    badge = scrapy.Field()
    # 一句话介绍
    headline = scrapy.Field()
    # 粉丝数
    follower_count = scrapy.Field()
    # 回答问题数
    answer_count = scrapy.Field()
    # 撰写文章数
    articles_count = scrapy.Field()
    # 头像
    avatar_url = scrapy.Field()
    avatar_url_template = scrapy.Field()
    # id
    id = scrapy.Field()
    # 注册类型
    type = scrapy.Field()
    # 注册url
    url = scrapy.Field()
    # 主页地址,唯一识别码
    url_token = scrapy.Field()
    # 用户类型
    user_type = scrapy.Field()
# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html
import pymongo
# 项目管道用来处理得到的item信息,这里设置存储到MongoDB的class
class MongoPipeline(object):

    #初始化变量, 这里需要传入mongo_uri,mongo_db两个参数,这两个参数可以从类方法里面获得
    def __init__(self,mongo_uri,mongo_db):
        self.mongo_uri = mongo_uri
        self.mongo_db = mongo_db

    # 定义类方法,获得mongo_uri,mongo_db
    @classmethod
    def from_crawler(cls,crawler):
        return cls(
        mongo_uri = crawler.settings.get('MONGO_URI'),
        mongo_db = crawler.settings.get('MONGO_DB')
        )

    # 初始化mongodb的变量,client, 与db,爬虫启动时即开始初始化
    def open_spider(self,spider):
        self.client = pymongo.MongoClient(self.mongo_uri)
        self.db = self.client[self.mongo_db]

    # 存储主体进程,返回item或者DropItem,这里设置update方法设置去重, 如果有同名就更新,没有就重新建立
    def process_item(self, item, spider):
        name = item.__class__.__name__
        self.db[name].update({'url_token':item['url_token']}, {'$set':item}, True)
        return item

    # 关闭mongodb
    def close_spider(self,spider):
        self.client.close()
# -*- coding: utf-8 -*-

# Scrapy settings for zhihu2 project
#
# For simplicity, this file contains only settings considered important or
# commonly used. You can find more settings consulting the documentation:
#
#     http://doc.scrapy.org/en/latest/topics/settings.html
#     http://scrapy.readthedocs.org/en/latest/topics/downloader-middleware.html
#     http://scrapy.readthedocs.org/en/latest/topics/spider-middleware.html

BOT_NAME = 'zhihu2'

SPIDER_MODULES = ['zhihu2.spiders']
NEWSPIDER_MODULE = 'zhihu2.spiders'

MONGO_URI = 'localhost'
MONGO_DB = 'zhihu2'
# Crawl responsibly by identifying yourself (and your website) on the user-agent
#USER_AGENT = 'zhihu2 (+http://www.yourdomain.com)'

# Obey robots.txt rules
ROBOTSTXT_OBEY = False

# Configure maximum concurrent requests performed by Scrapy (default: 16)
#CONCURRENT_REQUESTS = 32

# Configure a delay for requests for the same website (default: 0)
# See http://scrapy.readthedocs.org/en/latest/topics/settings.html#download-delay
# See also autothrottle settings and docs
#DOWNLOAD_DELAY = 3
# The download delay setting will honor only one of:
#CONCURRENT_REQUESTS_PER_DOMAIN = 16
#CONCURRENT_REQUESTS_PER_IP = 16

# Disable cookies (enabled by default)
#COOKIES_ENABLED = False

# Disable Telnet Console (enabled by default)
#TELNETCONSOLE_ENABLED = False

# Override the default request headers:

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 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36',
  'authorization':'oauth c3cef7c66a1843f8b3a9e6a1e3160e20',
}

# Enable or disable spider middlewares
# See http://scrapy.readthedocs.org/en/latest/topics/spider-middleware.html
#SPIDER_MIDDLEWARES = {
#    'zhihu2.middlewares.Zhihu2SpiderMiddleware': 543,
#}

# Enable or disable downloader middlewares
# See http://scrapy.readthedocs.org/en/latest/topics/downloader-middleware.html
#DOWNLOADER_MIDDLEWARES = {
#    'zhihu2.middlewares.MyCustomDownloaderMiddleware': 543,
#}

# Enable or disable extensions
# See http://scrapy.readthedocs.org/en/latest/topics/extensions.html
#EXTENSIONS = {
#    'scrapy.extensions.telnet.TelnetConsole': None,
#}

# Configure item pipelines
# See http://scrapy.readthedocs.org/en/latest/topics/item-pipeline.html
ITEM_PIPELINES = {
   'zhihu2.pipelines.MongoPipeline': 300,
}

# Enable and configure the AutoThrottle extension (disabled by default)
# See http://doc.scrapy.org/en/latest/topics/autothrottle.html
#AUTOTHROTTLE_ENABLED = True
# The initial download delay
#AUTOTHROTTLE_START_DELAY = 5
# The maximum download delay to be set in case of high latencies
#AUTOTHROTTLE_MAX_DELAY = 60
# The average number of requests Scrapy should be sending in parallel to
# each remote server
#AUTOTHROTTLE_TARGET_CONCURRENCY = 1.0
# Enable showing throttling stats for every response received:
#AUTOTHROTTLE_DEBUG = False

# Enable and configure HTTP caching (disabled by default)
# See http://scrapy.readthedocs.org/en/latest/topics/downloader-middleware.html#httpcache-middleware-settings
#HTTPCACHE_ENABLED = True
#HTTPCACHE_EXPIRATION_SECS = 0
#HTTPCACHE_DIR = 'httpcache'
#HTTPCACHE_IGNORE_HTTP_CODES = []
#HTTPCACHE_STORAGE = 'scrapy.extensions.httpcache.FilesystemCacheStorage'

scrapy爬取全部知乎用户信息的更多相关文章

  1. Python爬虫从入门到放弃(十八)之 Scrapy爬取所有知乎用户信息(上)

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

  2. Python之爬虫(二十) Scrapy爬取所有知乎用户信息(上)

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

  3. Python爬虫从入门到放弃(十九)之 Scrapy爬取所有知乎用户信息(下)

    在上一篇文章中主要写了关于爬虫过程的分析,下面是代码的实现,完整代码在:https://github.com/pythonsite/spider items中的代码主要是我们要爬取的字段的定义 cla ...

  4. Python之爬虫(二十一) Scrapy爬取所有知乎用户信息(下)

    在上一篇文章中主要写了关于爬虫过程的分析,下面是代码的实现,完整代码在:https://github.com/pythonsite/spider items中的代码主要是我们要爬取的字段的定义 cla ...

  5. 利用Scrapy爬取所有知乎用户详细信息并存至MongoDB

    欢迎大家关注腾讯云技术社区-博客园官方主页,我们将持续在博客园为大家推荐技术精品文章哦~ 作者 :崔庆才 本节分享一下爬取知乎用户所有用户信息的 Scrapy 爬虫实战. 本节目标 本节要实现的内容有 ...

  6. 使用 Scrapy 爬取去哪儿网景区信息

    Scrapy 是一个使用 Python 语言开发,为了爬取网站数据,提取结构性数据而编写的应用框架,它用途广泛,比如:数据挖掘.监测和自动化测试.安装使用终端命令 pip install Scrapy ...

  7. 利用scrapy爬取腾讯的招聘信息

    利用scrapy框架抓取腾讯的招聘信息,爬取地址为:https://hr.tencent.com/position.php 抓取字段包括:招聘岗位,人数,工作地点,发布时间,及具体的工作要求和工作任务 ...

  8. 抓取百万知乎用户信息之HttpHelper的迭代之路

    什么是Httphelper? httpelpers是一个封装好拿来获取网络上资源的工具类.因为是用http协议,故取名httphelper. httphelper出现的背景 使用WebClient可以 ...

  9. 43.scrapy爬取链家网站二手房信息-1

    首先分析:目的:采集链家网站二手房数据1.先分析一下二手房主界面信息,显示情况如下: url = https://gz.lianjia.com/ershoufang/pg1/显示总数据量为27589套 ...

随机推荐

  1. ReactNative的基本组件的认识

    通过官网的react-native init myProject,并打开Android Studio的手机模拟器进行调试 下面的代码使用了 Text .Image.View.TextInput和的re ...

  2. IT连创业系列:产品设计之答题模块

    前言: 距上篇写完:IT连创业系列:新的一年,先淫文一篇! 转眼又两个星期了,今天不写文,估计大伙又得等两周了. 所以啊~~ 只能再努力一点了,花一天半天的,继续和大伙分享这让人心碎的创业历程. 这两 ...

  3. 【SSH/SFTP】SSH协议和SFTP

    [SSH和SFTP] ■ 设置一个只允许访问部分目录的SFTP服务器 由于SSH和SFTP之间的紧密联系,一个SFTP服务器必然会导致开放一定的SSH服务,而SSH的风险显然比SFTP要大一些.自然, ...

  4. 浅析Python多线程

    学习Python多线程的资料很多,吐槽Python多线程的博客也不少.本文主要介绍Python多线程实际应用,且假设读者已经了解多线程的基本概念.如果读者对进程线程概念不甚了解,可参见知名博主 阮一峰 ...

  5. servlet3.0注解loadOnStartup不起作用解决方案

    多次尝试3.0在源码中直接用注解配置loadOnStartup=1,即web应用启动时创建servlet实例,发现不起作用,但是在web.xml配置则可以正常运行.先上源码. package lee; ...

  6. JavaScript(第七天)【对象和数组】

    什么是对象,其实就是一种类型,即引用类型.而对象的值就是引用类型的实例.在ECMAScript中引用类型是一种数据结构,用于将数据和功能组织在一起.它也常被称做为类,但ECMAScript中却没有这种 ...

  7. 设计模式NO.1

    设计模式NO.1 根据作业要求完成下列题目: 题目1: (1)要求:某系统日志记录器要求支持多种日志记录方式,如文件记录.数据库记录等:用户可以根据要求动态选择日志记录方式.使用Factory模式来设 ...

  8. 201621123057 《Java程序设计》第7周学习总结

    1. 本周学习总结 1.1 思维导图:Java图形界面总结 1.2 可选:使用常规方法总结其他上课内容. 2.书面作业 1. GUI中的事件处理 1.1 写出事件处理模型中最重要的几个关键词. 答: ...

  9. Scrum 冲刺 第二日

    Scrum 冲刺 第二日 目录 要求 项目链接 燃尽图 问题 今日任务 明日计划 成员贡献量 要求 各个成员今日完成的任务(如果完成的任务为开发或测试任务,需给出对应的Github代码签入记录截图:如 ...

  10. JAVA的循环控制与循环嵌套

    循环控制和循环嵌套 循环控制是除了循环条件之外,控制循环是否进行的一个机制,这给处理循环问题带来了灵活性.循环体内的语句块可以是顺序执行的语句,可以是分支结构的语句,也可以是循环语句,循环中含循环,就 ...