本文整理汇总了Python中scrapy.spider.BaseSpider.set_crawler方法的典型用法代码示例。如果您正苦于以下问题:Python BaseSpider.set_crawler方法的具体用法?Python BaseSpider.set_crawler怎么用?Python BaseSpider.set_crawler使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类scrapy.spider.BaseSpider
的用法示例。
在下文中一共展示了BaseSpider.set_crawler方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_defaults_spider_mw
# 需要导入模块: from scrapy.spider import BaseSpider [as 别名]
# 或者: from scrapy.spider.BaseSpider import set_crawler [as 别名]
def get_defaults_spider_mw(self):
crawler = get_crawler()
spider = BaseSpider('foo')
spider.set_crawler(crawler)
defaults = dict([(k, [v]) for k, v in \
crawler.settings.get('DEFAULT_REQUEST_HEADERS').iteritems()])
return defaults, spider, DefaultHeadersMiddleware()
示例2: ManagerTestCase
# 需要导入模块: from scrapy.spider import BaseSpider [as 别名]
# 或者: from scrapy.spider.BaseSpider import set_crawler [as 别名]
class ManagerTestCase(TestCase):
settings_dict = None
def setUp(self):
self.crawler = get_crawler(self.settings_dict)
self.spider = BaseSpider('foo')
self.spider.set_crawler(self.crawler)
self.mwman = DownloaderMiddlewareManager.from_crawler(self.crawler)
# some mw depends on stats collector
self.crawler.stats.open_spider(self.spider)
return self.mwman.open_spider(self.spider)
def tearDown(self):
self.crawler.stats.close_spider(self.spider, '')
return self.mwman.close_spider(self.spider)
def _download(self, request, response=None):
"""Executes downloader mw manager's download method and returns
the result (Request or Response) or raise exception in case of
failure.
"""
if not response:
response = Response(request.url)
def download_func(**kwargs):
return response
dfd = self.mwman.download(download_func, request, self.spider)
# catch deferred result and return the value
results = []
dfd.addBoth(results.append)
self._wait(dfd)
ret = results[0]
if isinstance(ret, Failure):
ret.raiseException()
return ret
示例3: get_spider_and_mw
# 需要导入模块: from scrapy.spider import BaseSpider [as 别名]
# 或者: from scrapy.spider.BaseSpider import set_crawler [as 别名]
def get_spider_and_mw(self, default_useragent):
crawler = get_crawler({'USER_AGENT': default_useragent})
spider = BaseSpider('foo')
spider.set_crawler(crawler)
return spider, UserAgentMiddleware()
示例4: get_request_spider_mw
# 需要导入模块: from scrapy.spider import BaseSpider [as 别名]
# 或者: from scrapy.spider.BaseSpider import set_crawler [as 别名]
def get_request_spider_mw(self):
crawler = get_crawler()
spider = BaseSpider('foo')
spider.set_crawler(crawler)
request = Request('http://scrapytest.org/')
return request, spider, DownloadTimeoutMiddleware.from_crawler(crawler)