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

items中的代码主要是我们要爬取的字段的定义

  1. class UserItem(scrapy.Item):
  2. id = Field()
  3. name = Field()
  4. account_status = Field()
  5. allow_message= Field()
  6. answer_count = Field()
  7. articles_count = Field()
  8. avatar_hue = Field()
  9. avatar_url = Field()
  10. avatar_url_template = Field()
  11. badge = Field()
  12. business = Field()
  13. employments = Field()
  14. columns_count = Field()
  15. commercial_question_count = Field()
  16. cover_url = Field()
  17. description = Field()
  18. educations = Field()
  19. favorite_count = Field()
  20. favorited_count = Field()
  21. follower_count = Field()
  22. following_columns_count = Field()
  23. following_favlists_count = Field()
  24. following_question_count = Field()
  25. following_topic_count = Field()
  26. gender = Field()
  27. headline = Field()
  28. hosted_live_count = Field()
  29. is_active = Field()
  30. is_bind_sina = Field()
  31. is_blocked = Field()
  32. is_advertiser = Field()
  33. is_blocking = Field()
  34. is_followed = Field()
  35. is_following = Field()
  36. is_force_renamed = Field()
  37. is_privacy_protected = Field()
  38. locations = Field()
  39. is_org = Field()
  40. type = Field()
  41. url = Field()
  42. url_token = Field()
  43. user_type = Field()
  44. logs_count = Field()
  45. marked_answers_count = Field()
  46. marked_answers_text = Field()
  47. message_thread_token = Field()
  48. mutual_followees_count = Field()
  49. participated_live_count = Field()
  50. pins_count = Field()
  51. question_count = Field()
  52. show_sina_weibo = Field()
  53. thank_from_count = Field()
  54. thank_to_count = Field()
  55. thanked_count = Field()
  56. type = Field()
  57. vote_from_count = Field()
  58. vote_to_count = Field()
  59. voteup_count = Field()

这些字段的是在用户详细信息里找到的,如下图所示,这里一共有58个字段,可以详细研究每个字段代表的意思:

关于spiders中爬虫文件zhihu.py中的主要代码

这段代码是非常重要的,主要的处理逻辑其实都是在这里

  1. class ZhihuSpider(scrapy.Spider):
  2. name = "zhihu"
  3. allowed_domains = ["www.zhihu.com"]
  4. start_urls = ['http://www.zhihu.com/']
  5. #这里定义一个start_user存储我们找的大V账号
  6. start_user = "excited-vczh"
  7.  
  8. #这里把查询的参数单独存储为user_query,user_url存储的为查询用户信息的url地址
  9. user_url = "https://www.zhihu.com/api/v4/members/{user}?include={include}"
  10. 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"
  11.  
  12. #follows_url存储的为关注列表的url地址,fllows_query存储的为查询参数。这里涉及到offset和limit是关于翻页的参数,0,20表示第一页
  13. follows_url = "https://www.zhihu.com/api/v4/members/{user}/followees?include={include}&offset={offset}&limit={limit}"
  14. follows_query = "data%5B*%5D.answer_count%2Carticles_count%2Cgender%2Cfollower_count%2Cis_followed%2Cis_following%2Cbadge%5B%3F(type%3Dbest_answerer)%5D.topics"
  15.  
  16. #followers_url是获取粉丝列表信息的url地址,followers_query存储的为查询参数。
  17. followers_url = "https://www.zhihu.com/api/v4/members/{user}/followers?include={include}&offset={offset}&limit={limit}"
  18. followers_query = "data%5B*%5D.answer_count%2Carticles_count%2Cgender%2Cfollower_count%2Cis_followed%2Cis_following%2Cbadge%5B%3F(type%3Dbest_answerer)%5D.topics"
  19.  
  20. def start_requests(self):
  21. '''
  22. 这里重写了start_requests方法,分别请求了用户查询的url和关注列表的查询以及粉丝列表信息查询
  23. :return:
  24. '''
  25. yield Request(self.user_url.format(user=self.start_user,include=self.user_query),callback=self.parse_user)
  26. yield Request(self.follows_url.format(user=self.start_user,include=self.follows_query,offset=0,limit=20),callback=self.parse_follows)
  27. yield Request(self.followers_url.format(user=self.start_user,include=self.followers_query,offset=0,limit=20),callback=self.parse_followers)
  28.  
  29. def parse_user(self, response):
  30. '''
  31. 因为返回的是json格式的数据,所以这里直接通过json.loads获取结果
  32. :param response:
  33. :return:
  34. '''
  35. result = json.loads(response.text)
  36. item = UserItem()
  37. #这里循环判断获取的字段是否在自己定义的字段中,然后进行赋值
  38. for field in item.fields:
  39. if field in result.keys():
  40. item[field] = result.get(field)
  41.  
  42. #这里在返回item的同时返回Request请求,继续递归拿关注用户信息的用户获取他们的关注列表
  43. yield item
  44. yield Request(self.follows_url.format(user = result.get("url_token"),include=self.follows_query,offset=0,limit=20),callback=self.parse_follows)
  45. yield Request(self.followers_url.format(user = result.get("url_token"),include=self.followers_query,offset=0,limit=20),callback=self.parse_followers)
  46.  
  47. def parse_follows(self, response):
  48. '''
  49. 用户关注列表的解析,这里返回的也是json数据 这里有两个字段data和page,其中page是分页信息
  50. :param response:
  51. :return:
  52. '''
  53. results = json.loads(response.text)
  54.  
  55. if 'data' in results.keys():
  56. for result in results.get('data'):
  57. yield Request(self.user_url.format(user = result.get("url_token"),include=self.user_query),callback=self.parse_user)
  58.  
  59. #这里判断page是否存在并且判断page里的参数is_end判断是否为False,如果为False表示不是最后一页,否则则是最后一页
  60. if 'page' in results.keys() and results.get('is_end') == False:
  61. next_page = results.get('paging').get("next")
  62. #获取下一页的地址然后通过yield继续返回Request请求,继续请求自己再次获取下页中的信息
  63. yield Request(next_page,self.parse_follows)
  64.  
  65. def parse_followers(self, response):
  66. '''
  67. 这里其实和关乎列表的处理方法是一样的
  68. 用户粉丝列表的解析,这里返回的也是json数据 这里有两个字段data和page,其中page是分页信息
  69. :param response:
  70. :return:
  71. '''
  72. results = json.loads(response.text)
  73.  
  74. if 'data' in results.keys():
  75. for result in results.get('data'):
  76. yield Request(self.user_url.format(user = result.get("url_token"),include=self.user_query),callback=self.parse_user)
  77.  
  78. #这里判断page是否存在并且判断page里的参数is_end判断是否为False,如果为False表示不是最后一页,否则则是最后一页
  79. if 'page' in results.keys() and results.get('is_end') == False:
  80. next_page = results.get('paging').get("next")
  81. #获取下一页的地址然后通过yield继续返回Request请求,继续请求自己再次获取下页中的信息
  82. yield Request(next_page,self.parse_followers)

上述的代码的主要逻辑用下图分析表示:

关于上图的一个简单描述:
1. 当重写start_requests,一会有三个yield,分别的回调函数调用了parse_user,parse_follows,parse_followers,这是第一次会分别获取我们所选取的大V的信息以及关注列表信息和粉丝列表信息
2. 而parse分别会再次回调parse_follows和parse_followers信息,分别递归获取每个用户的关注列表信息和分析列表信息
3. parse_follows获取关注列表里的每个用户的信息回调了parse_user,并进行翻页获取回调了自己parse_follows
4. parse_followers获取粉丝列表里的每个用户的信息回调了parse_user,并进行翻页获取回调了自己parse_followers

通过上面的步骤实现所有用户信息的爬取,最后是关于数据的存储

关于数据存储到mongodb

这里主要是item中的数据存储到mongodb数据库中,这里主要的一个用法是就是插入的时候进行了一个去重检测

  1. class MongoPipeline(object):
  2.  
  3. def __init__(self, mongo_uri, mongo_db):
  4. self.mongo_uri = mongo_uri
  5. self.mongo_db = mongo_db
  6.  
  7. @classmethod
  8. def from_crawler(cls, crawler):
  9. return cls(
  10. mongo_uri=crawler.settings.get('MONGO_URI'),
  11. mongo_db=crawler.settings.get('MONGO_DATABASE', 'items')
  12. )
  13.  
  14. def open_spider(self, spider):
  15. self.client = pymongo.MongoClient(self.mongo_uri)
  16. self.db = self.client[self.mongo_db]
  17.  
  18. def close_spider(self, spider):
  19. self.client.close()
  20.  
  21. def process_item(self, item, spider):
  22. #这里通过mongodb进行了一个去重的操作,每次更新插入数据之前都会进行查询,判断要插入的url_token是否已经存在,如果不存在再进行数据插入,否则放弃数据
  23. self.db['user'].update({'url_token':item["url_token"]},{'$set':item},True)
  24. return item

Python爬虫从入门到放弃(十九)之 Scrapy爬取所有知乎用户信息(下)的更多相关文章

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

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

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

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

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

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

  4. python爬虫从入门到放弃(九)之 实例爬取上海高级人民法院网开庭公告数据

    通过前面的文章已经学习了基本的爬虫知识,通过这个例子进行一下练习,毕竟前面文章的知识点只是一个 一个单独的散知识点,需要通过实际的例子进行融合 分析网站 其实爬虫最重要的是前面的分析网站,只有对要爬取 ...

  5. python爬虫从入门到放弃(九)之 Requests+正则表达式爬取猫眼电影TOP100

    import requests from requests.exceptions import RequestException import re import json from multipro ...

  6. Python爬虫从入门到放弃(二十)之 Scrapy分布式原理

    关于Scrapy工作流程回顾 Scrapy单机架构 上图的架构其实就是一种单机架构,只在本机维护一个爬取队列,Scheduler进行调度,而要实现多态服务器共同爬取数据关键就是共享爬取队列. 分布式架 ...

  7. Python爬虫从入门到放弃(二十二)之 爬虫与反爬虫大战

    爬虫与发爬虫的厮杀,一方为了拿到数据,一方为了防止爬虫拿到数据,谁是最后的赢家? 重新理解爬虫中的一些概念 爬虫:自动获取网站数据的程序反爬虫:使用技术手段防止爬虫程序爬取数据误伤:反爬虫技术将普通用 ...

  8. python爬虫从入门到放弃(三)之 Urllib库的基本使用

    官方文档地址:https://docs.python.org/3/library/urllib.html 什么是Urllib Urllib是python内置的HTTP请求库包括以下模块urllib.r ...

  9. Python爬虫从入门到放弃(十一)之 Scrapy框架整体的一个了解

    这里是通过爬取伯乐在线的全部文章为例子,让自己先对scrapy进行一个整理的理解 该例子中的详细代码会放到我的github地址:https://github.com/pythonsite/spider ...

随机推荐

  1. 免费在线生成彩色带logo的个性二维码

          码工具网站提供免费的在线二维码生成服务,可以把网址.文本.电子邮件.短信.电话号码.电子名片.wifi网络等信息生成对应的二维码图片.你可以设置二维码图片的格式(png,jpg,gif). ...

  2. 常见web容器

    当前主流的还是tomcat,jetty有较大的潜力,缩小彼此间差距,

  3. JVM高级特性-一、java内存结构区域介绍

    区域划分: java虚拟机在执行程序的过程中,将内存分为功能不同的几个区域,如下图: 此图列出了内存划分的各个区域,其中 线程私有的:程序计数器.虚拟机栈.本地方法栈 线程共享的:堆.方法区 下面,逐 ...

  4. Java IO学习笔记六

    打印流 在整个IO包中,打印流是输出信息最方便的类,主要包含字节打印流(PrintStream)和字符打印流(PrintWrite).打印流提供了非常方便的打印功能,可以打印任何的数据类型,例如:小数 ...

  5. maven 的docker插件

    首先你得配置一个带有认证的docker私有仓库. 本机要安装maven和jdk vi pom.xml <plugin> <groupId>com.spotify</gro ...

  6. Hadoop 笔记1 (原理和HDFS分布式搭建)

    1. hadoop 是什么 以及解决的问题 (自行百度) 2.基本概念的讲解 1. NodeName  master 节点(NN) 主节点 保存了metaData(元数据信息) 包括文件的owener ...

  7. chrome谷歌浏览器-DevTool开发者工具-详细总结

    目录: 一.概述 1.官方文档 2.打开方法: 3.前言: 二.九个模块: 1.设备模式Device Mode 2.元素面板Elements 3.控制台面板Console 4.源代码面板Sources ...

  8. MHD simulation with python

    这里为MHD(磁流体力学)模拟做一些准备,可能现在学习物理的人从某种程度上也得变成程序猿吧.MHD模拟面临的问题是求解一个三维的偏微分方程组,其中涉及的流体元格点非常多.所以希望先从简单的一维模型出发 ...

  9. Css3视频教程下载

    本套教程主要讲解了大量的CSS3新功能,包括: 边框.圆角.背景.渐变.阴影.文本特效.2D/3D转换.过渡.动画.伪类元素的使用等,同时伴随了大量的实例制作,比如CSS3实现红心的制作,火焰字.多彩 ...

  10. PHP数字价格格式化,保留两位小数

    number_format(($v['cash']/100),2); demo=>9,271.15