如何从命令提示符将变量传递给在 scrapy 中执行的 lua 脚本?
How to pass a variable to lua script being executed inside scrapy from the command prompt?
我试图在scrapy中传递一个变量作为用户定义的参数,它将在lua脚本的for循环中使用,我的代码如下:
import scrapy
from scrapy_splash import SplashRequest
from scrapy.selector import Selector
class ProductsSpider(scrapy.Spider):
name = 'allproducts'
script = '''
function main(splash, args)
assert(splash:go(args.url))
assert(splash:wait(0.5))
result = {}
local upto = tonumber(splash.number)
for i=1,upto,1
do
#something
end
return output
end
'''
def start_requests(self):
url='https://medicalsupplies.co.uk'
yield SplashRequest(url=url, callback=self.parse, endpoint='render.html', args={'wait':0.5})
yield SplashRequest(url=url, callback=self.parse_other_pages, endpoint='execute',
args={'wait':0.5, 'lua_source':self.script, 'number':int(self.number)}, dont_filter=True)
def parse(self, response):
for tr in response.xpath("//table[@id='date']/tbody/tr"):
yield{
'output' : #something
}
def parse_other_pages(self,response):
for page in response.data:
sel=Selector(text=page)
for tr in sel.xpath("//table[@id='date']/tbody/tr"):
yield{
'output' : #something
}
所以,我面临的问题是,当我 运行 使用整数 lua 脚本的 for 循环时,即 for i=1,5,1
脚本工作正常但是当我尝试使用 scrapy crawl allproducts -a number=5 -o test.json
从命令提示符向脚本提供输入,同时对脚本内的 for 循环使用 for i=1,{self.number},1
,我的代码会抛出错误,我什至无法在这个字符串,有没有办法在不破坏代码的情况下将变量传递给文本字符串(这里称为脚本)?我知道我没有使用正确的语法,但我还没有找到任何相同的资源,感谢任何建议。
爬虫实际警告如下:
WARNING: Bad request to Splash: {'error': 400, 'type': 'ScriptError', 'description': 'Error happened while executing Lua script', 'info': {'source': '[string "..."]', 'line_number': 7, 'error': "attempt to index global 'self' (a nil value)", 'type': 'LUA_ERROR', 'message': 'Lua error: [string "..."]:7: attempt to index global \'self\' (a nil value)'}}
编辑 1:根据@Alexander 的建议,修改了 lua 脚本并将变量作为整数参数传递给 SplashRequest,还使用本地实例化了 lua 脚本中的变量(本地 upto = tonumber(splash.number))
现在的警告如下:
WARNING: Bad request to Splash: {'error': 400, 'type': 'ScriptError', 'description': 'Error happened while executing Lua script', 'info': {'source': '[string "..."]', 'line_number': 9, 'error': "'for' limit must be a number", 'type': 'LUA_ERROR', 'message WARNING: Bad request to Splash: {'error': 400, 'type': 'ScriptError', 'description': 'Error happened while executing Lua script', 'info': {'source': '[string "..."]', 'line_number': 9, 'error': "'for' limit must be a number", 'type': 'LUA_ERROR', 'message': 'Lua error: [string "..."]:9: \'for\' limit must be a number'}}
function main(splash, args)
没有 self
参数。然而第 5 行引用了它:for i=1,{self.number},1
。并且该函数不是用:
声明的方法(函数类型的Luatable的字段),其中self
是table.
你是说 splash
吗?
我认为,您应该在 Python 代码 (start_requests
) 中将 'number':self.number
添加到 args
,并将其称为 tonumber(args.number)
来自你的 Lua 脚本。
我试图在scrapy中传递一个变量作为用户定义的参数,它将在lua脚本的for循环中使用,我的代码如下:
import scrapy
from scrapy_splash import SplashRequest
from scrapy.selector import Selector
class ProductsSpider(scrapy.Spider):
name = 'allproducts'
script = '''
function main(splash, args)
assert(splash:go(args.url))
assert(splash:wait(0.5))
result = {}
local upto = tonumber(splash.number)
for i=1,upto,1
do
#something
end
return output
end
'''
def start_requests(self):
url='https://medicalsupplies.co.uk'
yield SplashRequest(url=url, callback=self.parse, endpoint='render.html', args={'wait':0.5})
yield SplashRequest(url=url, callback=self.parse_other_pages, endpoint='execute',
args={'wait':0.5, 'lua_source':self.script, 'number':int(self.number)}, dont_filter=True)
def parse(self, response):
for tr in response.xpath("//table[@id='date']/tbody/tr"):
yield{
'output' : #something
}
def parse_other_pages(self,response):
for page in response.data:
sel=Selector(text=page)
for tr in sel.xpath("//table[@id='date']/tbody/tr"):
yield{
'output' : #something
}
所以,我面临的问题是,当我 运行 使用整数 lua 脚本的 for 循环时,即 for i=1,5,1
脚本工作正常但是当我尝试使用 scrapy crawl allproducts -a number=5 -o test.json
从命令提示符向脚本提供输入,同时对脚本内的 for 循环使用 for i=1,{self.number},1
,我的代码会抛出错误,我什至无法在这个字符串,有没有办法在不破坏代码的情况下将变量传递给文本字符串(这里称为脚本)?我知道我没有使用正确的语法,但我还没有找到任何相同的资源,感谢任何建议。
爬虫实际警告如下:
WARNING: Bad request to Splash: {'error': 400, 'type': 'ScriptError', 'description': 'Error happened while executing Lua script', 'info': {'source': '[string "..."]', 'line_number': 7, 'error': "attempt to index global 'self' (a nil value)", 'type': 'LUA_ERROR', 'message': 'Lua error: [string "..."]:7: attempt to index global \'self\' (a nil value)'}}
编辑 1:根据@Alexander 的建议,修改了 lua 脚本并将变量作为整数参数传递给 SplashRequest,还使用本地实例化了 lua 脚本中的变量(本地 upto = tonumber(splash.number))
现在的警告如下:
WARNING: Bad request to Splash: {'error': 400, 'type': 'ScriptError', 'description': 'Error happened while executing Lua script', 'info': {'source': '[string "..."]', 'line_number': 9, 'error': "'for' limit must be a number", 'type': 'LUA_ERROR', 'message WARNING: Bad request to Splash: {'error': 400, 'type': 'ScriptError', 'description': 'Error happened while executing Lua script', 'info': {'source': '[string "..."]', 'line_number': 9, 'error': "'for' limit must be a number", 'type': 'LUA_ERROR', 'message': 'Lua error: [string "..."]:9: \'for\' limit must be a number'}}
function main(splash, args)
没有 self
参数。然而第 5 行引用了它:for i=1,{self.number},1
。并且该函数不是用:
声明的方法(函数类型的Luatable的字段),其中self
是table.
你是说 splash
吗?
我认为,您应该在 Python 代码 (start_requests
) 中将 'number':self.number
添加到 args
,并将其称为 tonumber(args.number)
来自你的 Lua 脚本。