我正在尝试在 IBM 云上使用 Scrapy 作为一项功能。我__main__.py
的如下:
class AutoscoutListSpider(scrapy.Spider):
name = "vehicles list"
def __init__(self, params, *args, **kwargs):
super(AutoscoutListSpider, self).__init__(*args, **kwargs)
make = params.get("make", None)
model = params.get("model", None)
mileage = params.get("mileage", None)
init_url = "https://www.autoscout24.be/nl/resultaten?sort=standard&desc=0&ustate=N%2CU&size=20&page=1&cy=B&mmvmd0={0}&mmvmk0={1}&kmto={2}&atype=C&".format(
model, make, mileage)
self.start_urls = [init_url]
def parse(self, response):
# Get total result on list load
init_total_results = int(response.css('.cl-filters-summary-counter::text').extract_first().replace('.', ''))
if init_total_results > 400:
yield {"message": "There are MORE then 400 results"}
else:
yield {"message": "There are LESS then 400 results"}
def main(params):
process = CrawlerProcess()
try:
runner = crawler.CrawlerRunner()
runner.crawl(AutoscoutListSpider, params)
d = runner.join()
d.addBoth(lambda _: reactor.stop())
reactor.run()
return {"Success ": main_result}
except Exception as e:
return {"Error ": e, "params ": params}
我将它作为 IBM 函数上传到了,这很好。
但问题是当我运行它时,在python console
我invoke IBM function
第一次执行它时,但如果我想第二次执行它,我会收到一个错误:
{'Error ': ReactorNotRestartable(), 'params ': {'make': '9', 'model': '1624', 'mileage': '2500'}}
它是这样调用的:
IBM:
ibmcloud wsk action invoke --result ascrawler --param make 9 --param model 1624 --param mileage 2500
Python控制台:
main({"make":"9", "model":"1624", "mileage":"2500"})
使用下一个代码,我尝试添加多次运行它的可能性,但没有成功。
runner = crawler.CrawlerRunner()
runner.crawl(AutoscoutListSpider, params)
d = runner.join()
d.addBoth(lambda _: reactor.stop())
reactor.run()
知道如何解决吗?