Lua脚本无法单击按钮

问题描述 投票:1回答:1

我正试图使用​​这个lua脚本从scaz-splash刮掉link的航班:

function main(splash)
                local waiting_time = 2 

                -- Go to the URL
                assert(splash:go(splash.args.url))
                splash:wait(waiting_time)

                -- Click on "Outgoing tab"
                local outgoing_tab = splash:select('#linkRealTimeOutgoing')
                outgoing_tab:mouse_click()
                splash:wait(waiting_time)

                -- Click on "More Flights" button
                local more_flights_btn = splash:select('#ctl00_rptOutgoingFlights_ctl26_divPaging > div.advanced.noTop > a')
                more_flights_btn:mouse_click()
                splash:wait(waiting_time)

                return splash:html()
end

从某种原因我得到这个错误:

'LUA_ERROR', 'message': 'Lua error: [string "..."]:16: attempt to index local \'more_flights_btn\' (a nil value)', 'error': "attempt to index local 'more_flights_btn' (a nil value)"}, 'type': 'ScriptError', 'description': 'Error happened while executing Lua script'}

有谁知道为什么会这样?还有谁知道我可以在哪里获得lua脚本与splash的集成?除了官方网站?

提前致谢!

web-scraping lua scrapy scrapy-splash
1个回答
1
投票

这看起来像是一个时间问题。我运行了你的Lua脚本几次,我只得到了一次错误。

只需等待更长时间才能获得按钮就足够了。但是,如果花费的时间变化很大并且您并不总是想等待全部时间,那么您可以尝试使用这样一个更智能的循环:

-- Click on "More Flights" button
local more_flights_btn
-- Wait up to 10 seconds:
for i=1,10 do
    splash:wait(1)
    more_flights_btn = splash:select('#ctl00_rptOutgoingFlights_ctl26_divPaging > div.advanced.noTop > a')
    if more_flights_btn then break end
    -- If it was not found we'll wait again.
end
© www.soinside.com 2019 - 2024. All rights reserved.