【问题标题】:how to tell Lua to click on "loading button" infinitely?如何告诉Lua无限点击“加载按钮”?
【发布时间】:2021-01-05 08:37:11
【问题描述】:

这是我第一次使用 splash 来抓取网站。我需要告诉 splash 单击一个按钮,以便在浏览器上加载其他元素。这种情况无限进行。然后我想让splash返回HTML代码,这样我就可以用我的蜘蛛刮掉它。加载按钮没有href,所以我不能使用分页。因此,我尝试编写一个启动脚本来做到这一点。但是当我用 splash 运行脚本时,似乎“btn”部分在返回的 HTML 中没有任何作用(每次只返回第一页的 HTML。)

这是我写的启动脚本:

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": "Error happened while executing Lua script",
    "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 '='"
    }
}

【问题讨论】:

    标签: web-scraping lua scrapy-splash


    【解决方案1】:

    如果不存在,请稍等片刻,然后重试。你可以对你的容器做同样的事情,而不是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()
    

    【讨论】:

    • 谢谢@Doyousketch2 的解释。我用您的代码替换了“btn”部分并运行启动。它没有任何错误。但同样,在响应页面上,我得到了网站第一页的 HTML。它不应该显示整个 HTML 文件吗?或者它总是显示第一页?对了,“html:String(length 44838)”和之前一样。
    • 对该网页的了解不够,无法告诉您为什么会这样。也许页面需要 cookie 才能导航。您可以尝试在返回语句中删除大括号 return splash:html()
    • 非常感谢,@Doyousketch2。我编辑了上面的帖子,并在“编辑”部分输入了我得到的错误。正如你所说,我还更改了启动脚本。我知道这可能看起来很愚蠢,但我没有找到任何调试器来解决我遇到的错误,其他人的类似问题对这个错误没有帮助。所以,我把它放在这里。再次感谢您。
    猜你喜欢
    • 2020-02-15
    • 2018-04-17
    • 2018-03-03
    • 1970-01-01
    • 2018-05-27
    • 2015-02-27
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多