首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从命令提示符将变量传递给在scrapy中执行的lua脚本?

如何从命令提示符将变量传递给在scrapy中执行的lua脚本?
EN

Stack Overflow用户
提问于 2020-10-12 09:26:28
回答 1查看 830关注 0票数 0

我试图在scrapy中作为用户定义参数传递一个变量,这个变量将用于lua脚本的for循环,我的代码如下所示:

代码语言:javascript
复制
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从命令提示符向脚本提供输入时,我的代码会抛出一个错误,我甚至无法在这个字符串上使用f- string,有没有办法在不破坏代码的情况下将变量传递给文本字符串(这里称为脚本)?我知道我没有使用正确的语法,但我没有找到任何相同的资源,感谢任何建议。

铲运机发出的实际警告如下:

代码语言:javascript
复制
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,还使用本地(local = tonumber(splash.number))在lua脚本中实例化该变量

现时的警告如下:

代码语言:javascript
复制
 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'}}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-10-12 09:43:37

function main(splash, args)没有self参数。然而,第5行指的是:for i=1,{self.number},1。并且函数不是用:声明的方法( Lua表的函数类型的字段),其中self是该表。

你是说splash

我认为,您应该在Python代码( 'number':self.number代码,start_requests)中将args添加到start_requests中,并将其称为Lua脚本中的tonumber(args.number)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64315102

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档