0

我正在尝试在 IBM 云上使用 Scrapy 作为一项功能。我__main__.py的如下:

import scrapy
from scrapy.crawler import CrawlerProcess

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:
        process.crawl(AutoscoutListSpider, params)
        process.start()
        return {"Success ": "The crawler (make: {0}, model: {1}, mileage: {2}) is successfully executed.".format(
            params['make'], params['model'], params['mileage'])}
    except Exception as e:
        return {"Error ": e, "params ": params}

添加此功能的整个过程如下:

  1. zip -r ascrawler.zip __main__.py common.py// 因此我创建了一个 zip 文件来上传它。(还有一个 common.py 文件。为了简单起见,我从这里删除了它。
  2. ibmcloud wsk action create ascrawler --kind python:3 ascrawler.zip// 创建函数并将其添加到云端
  3. ibmcloud wsk action invoke --blocking --result ascrawler --param make 9 --param model 1624 --param mileage 2500// 调用带参数的函数

执行第三步后,我得到如下结果:

{"Success ": "The crawler (make: 9, model: 1624, mileage: 2500) is successfully executed."}

因此我没有得到任何错误,但它根本没有出现在AutoscoutListSpider课堂上。为什么?

它也应该返回{"message": "There are MORE then 400 results"}。任何的想法?

当我从 python 控制台运行它时,如下所示:

main({"make":"9", "model":"1624", "mileage":"2500"})

它返回正确的结果:

{"message": "There are MORE then 400 results"}
{"Success ": "The crawler (make: 9, model: 1624, mileage: 2500) is successfully executed."}
4

1 回答 1

1

{"message": "There are MORE then 400 results"}在调用的激活日志中可用,而不是操作结果。

运行ibmcloud wsk action invoke命令后,检索上一次调用的激活标识符。

$ ibmcloud wsk activation list
activations
d13bd19b196d420dbbd19b196dc20d59 ascrawler
...

然后可以使用此激活标识符从调用期间写入的 stdout 和 stderr 检索所有控制台日志。

$ ibmcloud wsk activation logs d13bd19b196d420dbbd19b196dc20d59 | grep LESS
2018-06-29T08:27:11.094873294Z stderr: {'message': 'There are LESS then 400 results'}
于 2018-06-29T08:33:41.553 回答