如何 select 来自已 select 元素对象的元素

How to select elements from an already selected element object with splash

在使用 select 元素 splash:select 之后,你如何 select 在它下面找到作为子元素的所有锚元素?

我已经用 scrapy/splash 试过这个 lua 脚本:

function main(splash)
    assert(splash:go(splash.args.url))
    assert(splash:wait(0.9))

    local classlist = splash:select('.class-list')        
    local alinks = classlist:select_all('a')

    return {alinks=alinks}

end

但是我收到以下对 Splash 错误的错误请求:

{
    u'info':{
        u'line_number':12,
        u'message':        u'Lua error:[
            string "..."
        ]:12:attempt to call method \'select_all\' (a nil value)',
        u'type':u'LUA_ERROR',
        u'source':u'        [
            string "..."
        ]        ', u'        error':u"attempt to call method 'select_all' (a nil value)"
    },
    u'type':u'ScriptError',
    u'description':u'Error happened while executing Lua script',
    u'error':400
}

我已经确认 splash:select('.class-list') returns 是一个有效的元素对象。

正如您所说,splash:select('.class-list') returns 一个有效的元素对象。你的问题是元素对象don't have a select_all method; only the splash object does。相反,您需要将 splash:select_all 与不同的选择器一起使用。请尝试以下操作(但请注意,因为它未经测试)。

function main(splash)
    assert(splash:go(splash.args.url))
    assert(splash:wait(0.9))
    local alinks = splash:select_all('.class-list a')        
    return {alinks=alinks}
end