如何在Scrapy中从命令提示符中向执行的lua脚本传递变量?
2020-10-12 10:28:46
收藏:0
阅读:152
评论:1
我试图在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 i = 1,{self.number},1来循环内部的for loop,我的代码会抛出一个错误,我甚至不能在这个字符串上使用f字符串,有没有绕过在不破坏代码的情况下将变量传递给文本字符串(在这里称为脚本)的方法? 我知道我没有使用正确的语法,但是我没有找到任何相关的资源,非常感谢任何建议。
爬虫的实际警告如下:
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脚本中使用local(local 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'}}
点赞
评论区的留言会收到邮件通知哦~
推荐文章
- Lua 虚拟机加密load(string.dump(function)) 后执行失败问题如何解决
- 我想创建一个 Nginx 规则,禁止访问
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?

function main(splash, args)没有self参数。 然而,第5行引用了它:for i=1,{self.number},1。 而该函数不是用:声明的方法(Lua函数类型的Lua表的字段),其中self是该表。您是指
splash吗?我认为您应该在Python代码(
start_requests)中将'number':self.number添加到args中,然后从Lua脚本中引用它为tonumber(args.number)。