栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在Scrapy中爬行经过身份验证的会话

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在Scrapy中爬行经过身份验证的会话

请勿在中覆盖

parse
函数
CrawlSpider

使用时

CrawlSpider
,你不应覆盖此
parse
功能。这里的
CrawlSpider
文档中有一个警告:http : //doc.scrapy.org/en/0.14/topics/spiders.html#scrapy.contrib.spiders.Rule

这是因为使用

CrawlSpider,parse
(任何请求的默认回调)都会发送要由
Rules
处理的响应。

爬网前登录:

为了在Spider开始抓取之前进行某种初始化,你可以使用

InitSpider
(继承自
CrawlSpider
),并覆盖该
init_request
函数。蜘蛛初始化时以及开始爬行之前,将调用此函数。

为了让Spider开始抓取,你需要致电

self.initialized

你可以在此处阅读对此负责的代码(它具有有用的文档字符串)。

一个例子:

from scrapy.contrib.spiders.init import InitSpiderfrom scrapy.http import Request, FormRequestfrom scrapy.contrib.linkextractors.sgml import SgmllinkExtractorfrom scrapy.contrib.spiders import Ruleclass MySpider(InitSpider):    name = 'myspider'    allowed_domains = ['example.com']    login_page = 'http://www.example.com/login'    start_urls = ['http://www.example.com/useful_page/',       'http://www.example.com/another_useful_page/']    rules = (        Rule(SgmllinkExtractor(allow=r'-w+.html$'),  callback='parse_item', follow=True),    )    def init_request(self):        """This function is called before crawling starts."""        return Request(url=self.login_page, callback=self.login)    def login(self, response):        """Generate a login request."""        return FormRequest.from_response(response,         formdata={'name': 'herman', 'password': 'password'},         callback=self.check_login_response)    def check_login_response(self, response):        """Check the response returned by a login request to see if we are        successfully logged in.        """        if "Hi Herman" in response.body: self.log("Successfully logged in. Let's start crawling!") # Now the crawling can begin.. return self.initialized()        else: self.log("Bad times :(") # Something went wrong, we couldn't log in, so nothing happens.    def parse_item(self, response):        # Scrape data from page


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/371013.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号