请勿在中覆盖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保存项目:
您的Spider返回的项目将传递到管道,该管道负责执行您想对数据进行的任何处理。我建议您阅读文档:http : //doc.scrapy.org/en/0.14/topics/item-
pipeline.html
如果您有关于
Items的任何问题/疑问,请随时打开一个新问题,我们将尽最大努力为您提供帮助。



