本周六结束了线下课程,转为线上,更加努力,fighting!
今天记录一下我在学习scrapy爬取知乎用户详细信息中遇到的一个bug。
我做的事情是请求某大V的基本信息,然后获取某大V的关注列表了,首先构造一个格式化的 url,将一些可变参数提取出来,然后需要重写 start_requests 方法,生成第一步的请求,接下来还需要根据获取到到关注列表做进一步的分析。
import json
from scrapy import Spider, Request
from zhihuuser.items import UserItem
class ZhihuSpider(Spider):
name = "zhihu"
allowed_domains = ["www.zhihu.com"]
user_url = 'https://www.zhihu.com/api/v4/members/{user}?include={include}'
follows_url = 'https://www.zhihu.com/api/v4/members/{user}/followees?include={include}&offset={offset}&limit={limit}'
start_user = 'excited-vczh'
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,answer_count,articles_count,pins_count,question_count,commercial_question_count,favorite_count,favorited_count,logs_count,marked_answers_count,marked_answers_text,message_thread_token,account_status,is_active,is_force_renamed,is_bind_sina,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_query = 'data[*].answer_count,articles_count,gender,follower_count,is_followed,is_following,badge[?(type=best_answerer)].topics'
def start_requests(self):
yield Request(self.user_url.format(user=self.start_user, include=self.user_query), self.parse_user)
yield Request(self.follows_url.format(user=self.start_user, include=self.follows_query, limit=20, offset=0),
self.parse_follows)
然后实现一下两个解析方法 parse_user 和 parse_follows
def parse_user(self, response):
print(response.text)
def parse_follows(self, response):
print(response.text)
最简单的实现他们的结果输出即可,然后运行观察结果。
scrapy crawl zhihu
这时发现出现了
401 HTTP status code is not handled or not allowed
访问被禁止了,这时观察下浏览器请求,发现它相比之前的请求多了一个 OAuth 请求头。
它是 Open Authorization 的缩写。 OAUTH_token:OAUTH 进行到最后一步得到的一个 “令牌”,通过此 “令牌” 请求,就可以去拥有资源的网站抓取任意有权限可以被抓取的资源。 在这里我知乎并没有登陆,这里的 OAuth 值是
oauth c3cef7c66a1843f8b3a9e6a1e3160e20
经过我长久的观察,这个一直不会改变,所以可以长久使用,我们将它配置到 DEFAULT_REQUEST_HEADERS 里,这样它就变成了:
DEFAULT_REQUEST_HEADERS = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36',
'authorization': 'oauth c3cef7c66a1843f8b3a9e6a1e3160e20',
}
接下来如果我们重新运行爬虫,就可以发现可以正常爬取了。