本文整理匯總了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)