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

classZhihuSpider(scrapy.Spider):

name= "zhihu"allowed_domains= ["www.zhihu.com"]

start_urls= ['http://www.zhihu.com/']#这里定义一个start_user存储我们找的大V账号

start_user = "excited-vczh"

#这里把查询的参数单独存储为user_query,user_url存储的为查询用户信息的url地址

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"

#follows_url存储的为关注列表的url地址,fllows_query存储的为查询参数。这里涉及到offset和limit是关于翻页的参数,0,20表示第一页

follows_url = "https://www.zhihu.com/api/v4/members/{user}/followees?include={include}&offset={offset}&limit={limit}"follows_query= "data%5B*%5D.answer_count%2Carticles_count%2Cgender%2Cfollower_count%2Cis_followed%2Cis_following%2Cbadge%5B%3F(type%3Dbest_answerer)%5D.topics"

#followers_url是获取粉丝列表信息的url地址,followers_query存储的为查询参数。

followers_url = "https://www.zhihu.com/api/v4/members/{user}/followers?include={include}&offset={offset}&limit={limit}"followers_query= "data%5B*%5D.answer_count%2Carticles_count%2Cgender%2Cfollower_count%2Cis_followed%2Cis_following%2Cbadge%5B%3F(type%3Dbest_answerer)%5D.topics"

defstart_requests(self):'''这里重写了start_requests方法,分别请求了用户查询的url和关注列表的查询以及粉丝列表信息查询

:return:'''

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)defparse_user(self, response):'''因为返回的是json格式的数据,所以这里直接通过json.loads获取结果

:param response:

:return:'''result=json.loads(response.text)

item=UserItem()#这里循环判断获取的字段是否在自己定义的字段中,然后进行赋值

for field initem.fields:if field inresult.keys():

item[field]=result.get(field)#这里在返回item的同时返回Request请求,继续递归拿关注用户信息的用户获取他们的关注列表

yielditemyield Request(self.follows_url.format(user = result.get("url_token"),include=self.follows_query,offset=0,limit=20),callback=self.parse_follows)yield Request(self.followers_url.format(user = result.get("url_token"),include=self.followers_query,offset=0,limit=20),callback=self.parse_followers)defparse_follows(self, response):'''用户关注列表的解析,这里返回的也是json数据 这里有两个字段data和page,其中page是分页信息

:param response:

:return:'''results=json.loads(response.text)if 'data' inresults.keys():for result in results.get('data'):yield Request(self.user_url.format(user = result.get("url_token"),include=self.user_query),callback=self.parse_user)#这里判断page是否存在并且判断page里的参数is_end判断是否为False,如果为False表示不是最后一页,否则则是最后一页

if 'page' in results.keys() and results.get('is_end') ==False:

next_page= results.get('paging').get("next")#获取下一页的地址然后通过yield继续返回Request请求,继续请求自己再次获取下页中的信息

yieldRequest(next_page,self.parse_follows)defparse_followers(self, response):'''这里其实和关乎列表的处理方法是一样的

用户粉丝列表的解析,这里返回的也是json数据 这里有两个字段data和page,其中page是分页信息

:param response:

:return:'''results=json.loads(response.text)if 'data' inresults.keys():for result in results.get('data'):yield Request(self.user_url.format(user = result.get("url_token"),include=self.user_query),callback=self.parse_user)#这里判断page是否存在并且判断page里的参数is_end判断是否为False,如果为False表示不是最后一页,否则则是最后一页

if 'page' in results.keys() and results.get('is_end') ==False:

next_page= results.get('paging').get("next")#获取下一页的地址然后通过yield继续返回Request请求,继续请求自己再次获取下页中的信息

yield Request(next_page,self.parse_followers)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
目标检测(Object Detection)是计算机视觉领域的一个核心问题,其主要任务是找出图像中所有感兴趣的目标(物体),并确定它们的类别和位置。以下是对目标检测的详细阐述: 一、基本概念 目标检测的任务是解决“在哪里?是什么?”的问题,即定位出图像中目标的位置并识别出目标的类别。由于各类物体具有不同的外观、形状和姿态,加上成像时光照、遮挡等因素的干扰,目标检测一直是计算机视觉领域最具挑战性的任务之一。 二、核心问题 目标检测涉及以下几个核心问题: 分类问题:判断图像中的目标属于哪个类别。 定位问题:确定目标在图像中的具体位置。 大小问题:目标可能具有不同的大小。 形状问题:目标可能具有不同的形状。 三、算法分类 基于深度学习的目标检测算法主要分为两大类: Two-stage算法:先进行区域生成(Region Proposal),生成有可能包含待检物体的预选框(Region Proposal),再通过卷积神经网络进行样本分类。常见的Two-stage算法包括R-CNN、Fast R-CNN、Faster R-CNN等。 One-stage算法:不用生成区域提议,直接在网络中提取特征来预测物体分类和位置。常见的One-stage算法包括YOLO系列(YOLOv1、YOLOv2、YOLOv3、YOLOv4、YOLOv5等)、SSD和RetinaNet等。 四、算法原理 以YOLO系列为例,YOLO将目标检测视为回归问题,将输入图像一次性划分为多个区域,直接在输出层预测边界框和类别概率。YOLO采用卷积网络来提取特征,使用全连接层来得到预测值。其网络结构通常包含多个卷积层和全连接层,通过卷积层提取图像特征,通过全连接层输出预测结果。 五、应用领域 目标检测技术已经广泛应用于各个领域,为人们的生活带来了极大的便利。以下是一些主要的应用领域: 安全监控:在商场、银行
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值