我正在使用带有 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 内容,我看不到数字
最佳答案
您正在打印 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 - 如何处理来自 splash 的 scrapy 中的多个返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37203458/