如何告诉Lua无限点击"加载按钮"?

这是我第一次使用Splash爬取网站。我需要告诉Splash点击一个按钮,以便在浏览器中加载其他元素。这无限期进行。然后我希望Splash返回HTML代码,以便我可以用我的蜘蛛爬取它。加载按钮没有href,所以我无法使用分页。因此,我试图编写一个Splash脚本来做到这一点。但是,当我用Splash运行脚本时,似乎"btn"部分在返回的HTML中没有起任何作用(每次仅返回第一页的HTML)。

这是我写的Splash脚本:

function main(splash,args)

    local function wait_for(it)
        item=splash:select(it)
        while not item:visible() do
            splash:wait(0.25)
            item=splash:select(it)
            return item
        end
    end

    splash.private_mode_enabled=false
    local head={'User-Agent','Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome'}
    assert(splash:go(args.url,headers=head))

    selector='.undefined.btn.small-Font'
    wait_for(selector):mouse_click()

    selector='.rtl.custom-container.pb-5'
    wait_for(selector):mouse_click()

    return splash:html()

end

有人能帮我理解如何告诉Splash"在"加载按钮"存在时,按下它,然后一次性返回整个HTML"吗?

顺便说一下,这是我想要爬取的非英语URL: http://namlik.me/channels

非常感谢!!

---编辑---

这是我在响应页面上收到的错误:

{
    "error": 400,
    "type": "ScriptError",
    "description": "执行Lua脚本时发生错误",
    "info": {
        "source": "[string \"function main(splash,args)\r...\"]",
        "line_number": 14,
        "error": "')' expected near '='",
        "type": "LUA_INIT_ERROR",
        "message": "[string \"function main(splash,args)\r...\"]:14: ')' expected near '='"
    }
}
点赞
用户3342050
用户3342050

如果它不存在,请等待一会儿,然后重试。您可以像splash:wait(10)一样使用自己的容器。

https://splash.readthedocs.io/en/stable/scripting-element-object.html#element-visible

btn = splash:select(".undefined.btn.small-Font")
visible = btn:visible()
while not visible do
    splash:wait(0.25)
    btn = splash:select(".undefined.btn.small-Font")
    visible = btn:visible()
end
btn:mouse_click()


等待例程可以是一个函数。

function main(splash, args)

    local function wait_for(it)
        item = splash:select(it)
        while not item:visible() do
            splash:wait(0.25)
            item = splash:select(it)
        end  -- visible?
        return item
    end  -- wait_for()

    splash.private_mode_enabled = false
    local head = {'User-Agent', 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome'}
    splash:set_user_agent(head)
    assert(splash:go(args.url))

    selector = '.undefined.btn.small-Font'
    wait_for(selector):mouse_click()

    selector = '.rtl.custom-container.pb-5'
    wait_for(selector):mouse_click()

    return splash:html()

end  -- main()
2020-11-25 20:34:35