将 ScrapySplash Lua 脚本中的表传递到 Python

我正在运行 ScrapySplash,尝试将在Lua中创建的 table 传递给 Parse 方法。如果我尝试访问该表,则会出现类型错误,显示:“'SplashJsonResponse' 对象不可下标”。

我的 Lua 脚本如下:

    script = '''
            function main(splash, args)
              assert(splash:go(args.url))
              assert(splash:wait(0.5))
              img = {}
              for i=1,5 do
                table.insert(img, "Testing")
              end

              return {
                html = splash:html(),
                png = splash:png(),
                har = splash:har(),
                img,
              }
            end
    '''

然后我的parse方法只是:

    def parse_item(self, response):
        images = response['img']
        print (images)

但是,如上所述,这只会在行中images = response ['img']上给出错误,显示'SplashJsonResponse'对象不可下标。

有人知道如何在parse方法中访问此Lua表中的值吗?

谢谢

点赞
用户10600051
用户10600051

,不知何故,我解决了这个问题,但我不知道如何解决。基本上,我在我的 Lua 语句中更改了返回语句如下:

  script ='''
            function main(splash, args)
              assert(splash:go(args.url))
              assert(splash:wait(0.5))
              img = {}
              for i=1,5 do
                table.insert(img, "Testing")
              end

              return {
                html = splash:html(),
                png = splash:png(),
                har = splash:har(),
                img,
              }
            end
    '''

然后在我的 parse 方法中有以下内容:

    def parse(self, response):
        images = (response.data['a'])

现在我有了一个名为 images 的字典,可以通过 images ['i'] 访问每个变量,尽管我不知道为什么这样做可以使我的原始方法无效。如果有人明白为什么,请随时评论并让我知道。谢谢。

2020-03-25 18:47:51
用户2632214
用户2632214

发生错误“SplashJsonResponse”对象不可下标化是因为您尝试从字典回复中访问键,而没有data键。

不正确: response['some_key_from_dictionary']

正确: 不正确:response.data['some_key_from_dictionary']

2021-08-04 15:47:12