以下列出运行环境与主要模块:

  • macOS 10.13.4
  • Chrome/JSON-handle
  • Scrapy 1.5.0
  • Abuyun HTTP tunnel(服务器:http-dyn.abuyun.com,端口:9020)
  • MongoDB shell version v4.0.0
  • 目标站点分析
  • 代码分析
  • 参考网址

目标站点分析

vczh轮子哥个人主页

  • 爬取思路

    • 以粉丝数较多的用户主页作为基准点;
    • 分别爬取其followees和followers列表下用户的信息;
    • 针对每个用户获取各自主页,重复上述步骤,迭代;
  • 注意点
    • 用户列表信息的翻页处理;
    • 递归终止条件;

代码分析

  • 根据需爬取用户的信息,创建Item类
# items.pyfrom scrapy import Fieldclass UserItem(scrapy.Item):id = Field()name = Field()account_status = Field()answer_count = Field()articles_count = Field()avatar_url = Field()badge = Field()#...vote_to_count = Field()voteup_count = Field()
  • 使用阿布云动态代理池,防止IP被403
# middlewares.py
import base64proxyServer = "http://http-dyn.abuyun.com:9020"
proxyUser = "myUser"
proxyPass = "myPassword"
proxyAuth = "Basic " + base64.urlsafe_b64encode(bytes((proxyUser + ":" + proxyPass), "ascii")).decode("utf8")class ProxyMiddleware(object):def process_request(self, request, spider):request.meta["proxy"] = proxyServerrequest.headers["Proxy-Authorization"] = proxyAuth
  • 连接MongoDB服务器
# pipelines.pyimport pymongoclass MongoPipeline(object):collection_name = 'users'def __init__(self, mongo_uri, mongo_db):self.mongo_uri = mongo_uriself.mongo_db = mongo_db@classmethoddef from_crawler(cls, crawler):return cls(mongo_uri=crawler.settings.get('MONGO_URI'),mongo_db=crawler.settings.get('MONGO_DATABASE'))def open_spider(self, spider):self.client = pymongo.MongoClient(self.mongo_uri)self.db = self.client[self.mongo_db]def close_spider(self, spider):self.client.close()def process_item(self, item, spider):self.db[self.collection_name].update({'url_token': item['url_token']}, {'$set': item}, upsert=True)return item
  • 为了便于阅读,仅在此列出已安装的配置,且微调顺序
# settings.py(部分)# 基本配置 + headers
BOT_NAME = 'zhihuuser'
SPIDER_MODULES = ['zhihuuser.spiders']
NEWSPIDER_MODULE = 'zhihuuser.spiders'
ROBOTSTXT_OBEY = False
USER_AGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'
DEFAULT_REQUEST_HEADERS = {'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8','Accept-Language': 'en',
}# 安装ProxyMiddleware,并根据代理的更替速度,设置下载延迟
DOWNLOADER_MIDDLEWARES = {'zhihuuser.middlewares.ProxyMiddleware': 543,
}
DOWNLOAD_DELAY = 0.1
AUTOTHROTTLE_ENABLED = True
AUTOTHROTTLE_START_DELAY = 0.1# 安装MongoPipeline,设置MongoDB的URI和DATABASE
ITEM_PIPELINES = {'zhihuuser.pipelines.MongoPipeline': 300,
}
MONGO_URI = 'localhost'
MONGO_DATABASE = 'zhihu'
  • 主程序如下
# spiders.zhihu.pyimport json
import scrapy
from scrapy import Request
from ..items import UserItemclass ZhihuSpider(scrapy.Spider):name = 'zhihu'allowed_domains = ['www.zhihu.com']start_user = 'excited-vczh'user_url = 'https://www.zhihu.com/api/v4/members/{user}?include={include}'user_query = 'locations,employments,gender,educations,business,voteup_count,thanked_Count,follower_count,following_count,cover_url,following_topic_count,following_question_count,following_favlists_count,following_columns_count,avatar_hue,answer_count,articles_count,pins_count,question_count,columns_count,commercial_question_count,favorite_count,favorited_count,logs_count,marked_answers_count,marked_answers_text,message_thread_token,account_status,is_active,is_bind_phone,is_force_renamed,is_bind_sina,is_privacy_protected,sina_weibo_url,sina_weibo_name,show_sina_weibo,is_blocking,is_blocked,is_following,is_followed,mutual_followees_count,vote_to_count,vote_from_count,thank_to_count,thank_from_count,thanked_count,description,hosted_live_count,participated_live_count,allow_message,industry_category,org_name,org_homepage,badge[?(type=best_answerer)].topics'followees_url = 'https://www.zhihu.com/api/v4/members/{user}/followees?include={include}&offset={offset}&limit={limit}'followees_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(url=self.user_url.format(user=self.start_user, include=self.user_query), callback=self.parse_user)yield Request(url=self.followees_url.format(user=self.start_user, include=self.followees_query, offset=0, limit=20), callback=self.parse_followees)# yield Request(url=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 = UserItem()for field in item.fields:if field in result.keys():item[field] = result.get(field)yield item# yield Request(url=self.followees_url.format(user=result.get('url_token'), include=self.followees_query, offset=0, limit=20), callback=self.parse_followees)# yield Request(url=self.followers_url.format(user=result.get('url_token'), include=self.followers_query, offset=0, limit=20), callback=self.parse_followers)def parse_followees(self, response):results = json.loads(response.text)if 'data' in results.keys():for result in results.get('data'):yield Request(url=self.user_url.format(user=result.get('url_token'), include=self.user_query), callback=self.parse_user)if 'paging' in results.keys() and not results.get('paging').get('is_end'):yield Request(url=results.get('paging').get('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'):#             yield Request(url=self.user_url.format(user=result.get('url_token'), include=self.user_query), callback=self.parse_user)#     if 'paging' in results.keys() and not results.get('paging').get('is_end'):#         yield Request(url=results.get('paging').get('next'), callback=self.parse_followers)

参考网址

收纳本次爬虫的重要参考网址,以便日后翻阅:

write-items-to-mongodb(官方)
阿布云介入指南(官方)

Scrapy爬取知乎用户信息(代理池,MongoDB,非分布式)相关推荐

  1. Scrapy爬取知乎用户信息以及人际拓扑关系

    Scrapy爬取知乎用户信息以及人际拓扑关系 1.生成项目 scrapy提供一个工具来生成项目,生成的项目中预置了一些文件,用户需要在这些文件中添加自己的代码. 打开命令行,执行:scrapy sta ...

  2. Scrapy爬取知乎用户信息

    1 爬取逻辑 先选取一个用户,爬取他的粉丝列表和关注列表.然后对每个粉丝进行分析,找出他们的粉丝列表和关注列表,以此往复,递归下去,就可以爬取大部分的用户信息了.通过一个树形的结构,蔓延到所有的用户. ...

  3. python爬虫实战笔记---以轮子哥为起点Scrapy爬取知乎用户信息

    开发环境:python3.5+Scrapy+pycharm+mongodb 思路: 1.选定起始人:选定一个关注数量或粉丝数量多的大佬 2.获取粉丝和关注列表 3.获取列表用户信息 4.获取每位用户粉 ...

  4. python爬取知乎文章_大佬分享Python编程实现爬取知乎用户信息的例子

    有一天 , 我发现我心仪已久的妹子在朋友圈里分享了知乎专栏的文章 , 就知道她也刷知乎 . 如果在知乎上关注她 , 我就能知道 , 她最近关注什么 , 心里想些什么 , 了解了解她喜欢的方面 , 还能 ...

  5. Scrapy实战:爬取知乎用户信息

    思路:从一个用户(本例为"张佳玮")出发,来爬取其粉丝,进而爬取其粉丝的粉丝- 先来观察网页结构: 审查元素: 可以看到用户"关注的人"等信息在网页中用json ...

  6. Python爬虫爬取知乎用户信息+寻找潜在客户

    [Python应用]寻找社交网络中的目标用户 日后的更新:由于是很久以前的课程设计项目,完整的源码已经不见了,关键的网页数据获取和解析的部分代码我在文章中已经贴出来了,但写的也不够好,如果想参考爬取知 ...

  7. python爬取知乎用户信息泄露_scrapy实战--爬取知乎用户信息(上)

    背景 使用Scrapy分布式爬取知乎所有用户个人信息! 项目地址 爬取知乎所有用户 大规模抓取静态网页Scrapy绝对是利器!当然也可以使用requests库来自己实现,但是要自己写过滤器等组件,既然 ...

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

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

  9. python爬虫知识点总结(二十四)Scrapy爬去知乎用户信息

    待更新 转载于:https://www.cnblogs.com/cthon/p/9424562.html

最新文章

  1. 【备忘】linux shell 字符串操作(长度,查找,替换,匹配)详解
  2. 中国电信与华为签物联网合作协议
  3. Bootstrap学习笔记之Nestable可拖拽树结构
  4. oracle数据库dca,有关Oracle数据库
  5. Window操作系统注册表学习
  6. 算法分析神器—时间复杂度
  7. Java | 内部类的实例化
  8. PS钢笔工具使用方法简介
  9. leetcode 没有php,Leetcode PHP题解--D99 860. Lemonade Change
  10. 虚幻开发工具包发布版本的版本信息
  11. 材料分享主题一:如何向上级汇报部门/组织架构
  12. 成为测试/开发程序员,小张:现实就来了个下马威......
  13. 韬韬抢苹果 #普及组#
  14. 带你解锁蓝牙skill(二)
  15. g700刷机包android5,华为G700线刷刷机教程_华为G700线刷rom包_救砖系统刷机包
  16. 4G:SIM7600CE-CNSE 4G模组调试
  17. Vista BitLocker 驱动器加密原理
  18. python高频交易策略_VNPY中 Tick级别准高频交易简单策略
  19. 牛客编程巅峰赛S2第10场 - 青铜白银黄金题解报告
  20. 抖音小程序Tiktok开发教程之 基础组件 02 rich-text 富文本组件

热门文章

  1. 长安大学计算机研究生报录比,2020年长安大学考研报录比
  2. 该不该动手术校正近视?
  3. 本地机器 Google Colab 通过 SSH 连接远程服务器
  4. java yearmonth_Java Year atMonth(Month month)用法及代码示例
  5. 【pytest】(二) pytest与unittest的比较
  6. 微信注册筛选软件 微信开通筛选技术
  7. 小学计算机课教师教学笔记,小学信息技术教学随笔
  8. 云端服务器维护,云端服务器维护
  9. 微信公众号发送消息 Java
  10. 基于 jQuery 的前端 UI 框架 LuLu UI