东莞网站开发,公众号的网站怎么做的,长沙seo在哪,加油优惠卡app软件开发思路分析#xff1a;(1)选定起始人(即选择关注数和粉丝数较多的人--大V)(2)获取该大V的个人信息(3)获取关注列表用户信息(4)获取粉丝列表用户信息(5)重复(2)(3)(4)步实现全知乎用户爬取实战演练#xff1a;(1)、创建项目#xff1a;scrapy startproject zhijutest(2)、创建爬…思路分析(1)选定起始人(即选择关注数和粉丝数较多的人--大V)(2)获取该大V的个人信息(3)获取关注列表用户信息(4)获取粉丝列表用户信息(5)重复(2)(3)(4)步实现全知乎用户爬取实战演练(1)、创建项目scrapy startproject zhijutest(2)、创建爬虫cd zhihutest -----scrapy genspider zhihu www.zhihu.com(3)、选取起始人(这里我选择了以下用户)我们可以看到他关注的人和关注他的人这些内容是我们(3)(4)步需要获取的(3)、更改settings.py代码分析这里我们设置了不遵守robots协议robots协议网络爬虫协议它用来告诉用户那些内容可以爬取那些内容禁止爬取一般我们运行爬虫项目首先会访问网站的robots.txt页面它告诉爬虫那些是你可以获取的内容这里我们为了方便即不遵守robots协议。代码分析这里我们设置了User-Agent和authorization字段(这是知乎对请求头的限制了即反爬)而这里我们通过设置模拟了在没有登陆的前提下伪装成浏览器去请求知乎(4)、页面初步分析右击鼠标打开chrome开发者工具选项并选中如下箭头所指将鼠标放在黄色标记上我们可以发现右侧加载出了一个ajax请求单击该ajax请求得到如下页面我们可以看见黄色部分为每位用户的详细信息的url,它包含多个参数用来存储信息此时再将页面下滑可以看到如下信息该字段为上面参数的字段详情(Query String Parameters英文好的小伙伴应该一眼发现)(5)、更改items.py承接上面将页面点击左侧并翻页可以看出右侧出现了新的Ajax请求followees:......这就是他关注者信息通过点击Preview我们获取了网页源代码可以发现包含了每一页的用户信息小伙伴们可以核对下发现信息能匹配上我们可以从中发现每页包含20条他的关注者信息而黑框部分就是包含每一位用户详细信息的参数我们通过它们来定义item.py(即爬什么)修改items.py如下(6)、更改zhihu.py第一步模块导入1 #-*- coding: utf-8 -*-2 importjson34 importscrapy56 from ..items importUserItem789 classZhihuSpider(scrapy.Spider):10 name zhihu11 allowed_domains [zhihu.com]12 start_urls [http://zhihu.com/]1314 #设定起始爬取人这里我们通过观察发现与url_token字段有关15 start_user zhouyuan1617 #选取起始爬取人的页面详情信息这里我们传入了user和include参数方便对不同的用户进行爬取18 user_url https://www.zhihu.com/api/v4/members/{user}?include{include}19 #用户详情参数即包含在include后面的字段20 user_query allow_message,is_followed,is_following,is_org,is_blocking,employments,answer_count,follower_count,articles_count,gender,badge[?(typebest_answerer)].topics2122 #这是他的关注者的url,这里包含了每位他的关注者的url同样我们传入了user和include参数方便对不同用户进行爬取23 follows_url https://www.zhihu.com/api/v4/members/{user}/followees?include{include}offset{offset}limit{limit}24 #他的每位关注者详情参数即包含在include后面的字段25 follows_query data[*].answer_count,articles_count,gender,follower_count,is_followed,is_following,badge[?(typebest_answerer)].topics2627 #这是他的粉丝的url,这里包含了每位他的关注者的url同样我们传入了user和include参数方便对不同用户进行爬取28 followers_url https://www.zhihu.com/api/v4/members/{user}/followees?include{include}offset{offset}limit{limit}29 #他的每位粉丝的详情参数即包含在include后面的字段30 followers_query data[*].answer_count,articles_count,gender,follower_count,is_followed,is_following,badge[?(typebest_answerer)].topics3132 #重新定义起始爬取点的url33 defstart_requests(self):34 #这里我们传入了将选定的大V的详情页面的url并指定了解析函数parseUser35 yield scrapy.Request(self.user_url.format(userself.start_user, includeself.user_query), callbackself.parseUser)36 #这里我们传入了将选定的大V他的关注者的详情页面的url并指定了解析函数parseFollows37 yield scrapy.Request(self.follows_url.format(userself.start_user, includeself.follows_query, offset0, limit20), callbackself.parseFollows)38 #这里我们传入了将选定的大V的粉丝的详情页面的url并指定了解析函数parseFollowers39 yield scrapy.Request(self.followers_url.format(userself.start_user, includeself.followers_query, offset0, limit20), callbackself.parseFollowers)4041 #爬取每一位用户详情的页面解析函数42 defparseUser(self, response):43 #这里页面上是json字符串类型我们使用json.loads()方法将其变为文本字符串格式44 result json.loads(response.text)45 item UserItem()4647 #这里我们遍历了items.py中定义的字段并判断每位用户的详情页中的keys是否包含该字段如包含则获取48 for field initem.fields:49 if field inresult.keys():50 item[field] result.get(field)51 yielditem52 #定义回调函数爬取他的关注者与粉丝的详细信息实现层层迭代53 yield scrapy.Request(self.follows_url.format(userresult.get(url_token), includeself.follows_query, offset0, limit20), callbackself.parseFollows)54 yield scrapy.Request(self.followers_url.format(userresult.get(url_token), includeself.followers_query, offset0, limit20), callbackself.parseFollowers)5556 #他的关注者的页面解析函数57 defparseFollows(self, response):58 results json.loads(response.text)59 #判断data标签下是否含有获取的文本字段的keys60 if data inresults.keys():61 for result in results.get(data):62 yield scrapy.Request(self.user_url.format(userresult.get(url_token), includeself.user_query), callbackself.parseUser)63 #判断页面是否翻到了最后64 if paging in results.keys() and results.get(paging).get(is_end) False:65 next_page results.get(paging).get(next)66 yield scrapy.Request(next_page, callbackself.parseFollows)6768 #他的粉丝的页面解析函数69 defparseFollowers(self, response):70 results json.loads(response.text)7172 if data inresults.keys():73 for result in results.get(data):74 yield scrapy.Request(self.user_url.format(userresult.get(url_token), includeself.user_query), callbackself.parseUser)7576 if paging in results.keys() and results.get(paging).get(is_end) False:77 next_page results.get(paging).get(next)78 yield scrapy.Request(next_page, callbackself.parseFollowers)我们可以看到当我们翻到了最后is_end字段变为了True,而next字段就是下一个页面的url(7)、运行下程序可以看见已经在爬取了(8)、将结果存入Mongodb数据库重写pipelines.py1 importpymongo23 classMongoPipeline(object):45 collection_name user67 def __init__(self, mongo_uri, mongo_db):8 self.mongo_uri mongo_uri9 self.mongo_db mongo_db1011 classmethod12 deffrom_crawler(cls, crawler):13 returncls(14 mongo_uricrawler.settings.get(MONGO_URI),15 mongo_dbcrawler.settings.get(MONGO_DATABASE)16 )1718 defopen_spider(self, spider):19 self.client pymongo.MongoClient(self.mongo_uri)20 self.db self.client[self.mongo_db]2122 defclose_spider(self, spider):23 self.client.close()2425 defprocess_item(self, item, spider):26 self.db[user].update({url_token :item[url_token]},{$set:item},True)代码分析我们创建了名为user的集合重写了__init__方法指定了数据库的链接地址和数据库名称并修改了工厂类函数(具体参见上讲ITEM PIPLELIEN用法)打开数据库并插入数据并以url_token字段对重复数据执行了更新操作最后我们关闭了数据库再配置下settings.py再次运行程序可以看见我们的数据就到了数据库了