【问题标题】:how to handle multiple return values in scrapy from splash如何从splash处理scrapy中的多个返回值
【发布时间】:2016-05-13 07:10:28
【问题描述】:

我正在使用带有 splash 的 scrapy,在我的 splash 中我可以发送多个值,但在我的 scrapy 代码中我无法处理所有内容。例如, 这是我的启动脚本

splash_script = """
    function main(splash)
      local url = splash.args.url
      return {
        html = splash:html(),
        number = 1
      }
    end
    """

从scrapy触发splash的方法

yield scrapy.Request(
              url= response.urljoin(url),
              callback = self.product_details,
              errback=self.error,
              dont_filter=True,
              meta = {
                'splash':{
                  'endpoint': 'render.html',
                  'cache_args': ['lua_source'],
                  'args' :{
                     'index': index,
                     'http_method':'GET',
                     'lua_source': self.splash_script,
                   }
               }
          },
        )

回调方法

def product_details(self,response):
    print response.body

这个方法只接收html内容,我看不到数字

【问题讨论】:

    标签: scrapy scrapy-spider splash-screen


    【解决方案1】:

    您正在打印 response.body 。这仅包括 html。

    您必须使用response.data 才能看到 1。

    您也可以单独访问元素:

    response.data['html'] 
    

    response.data['number'] 
    

    当你返回东西时,确保你在返回语句中分配它:

    不-

    html = splash:html()
    number = 1
    return {number,html}
    

    但是

    return {number = 1, html = splash:html()}
    

    基本上,您必须在 return 语句中分配 JSON 键,即使您可能已经在外面这样做了。 额外的信息,但这真的把我搞砸了,你可能会遇到同样的问题。

    【讨论】:

    • 不使用scrapy,数据不是response的属性怎么办?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-26
    • 2017-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多