【发布时间】:2019-08-07 05:49:50
【问题描述】:
我编写了我的scrapy spider,它以两个类变量开头,然后想从Runner 运行它。 我确实尝试过:
yield runner.crawl(MySpider1, variable1, variable2)
或
yield runner.crawl(MySpider1, [variable1, variable2])
或
yield runner.crawl(MySpider1, (variable1, variable2))
或
yield runner.crawl(MySpider1(variable1, variable2))
但是得到了
缺少 1 个必需的后置参数
这是我的代码:
from twisted.internet import reactor, defer
from scrapy.crawler import CrawlerRunner
from scrapy.utils.log import configure_logging
class MySpider(scrapy.Spider):
def _init__(self, variable1, variable2, *args, **kwargs):
super().__init__(*arg, **kwargs)
self.variable1 = variable1
self.variable2 = variable2
# below should be any normal spider's parser
class Run_Spider_From_SubClass(SpiderEmail):
def __init__(self, *args, **kwargs):
super().__init__(self, *args, **kwargs)
configure_logging()
self.runner = CrawlerRunner(get_project_settings())
@defer.inlineCallbacks
def crawl(self):
for variable1, variable2 in mydict.item():
yield self.runner.crawl(MySpider, variable1, varialbe2) # input issue that result in missing 1 positional argument
reactor.stop()
def run_spider_in_loop(self):
self.crawl()
reactor.run()
runner = Run_Spider_From_SubClass()
runner.run_spider_in_loop()
在 Runner 中输入蜘蛛变量的正确方法应该是什么?谢谢
【问题讨论】:
标签: python variables scrapy runner