Lua脚本单击按钮失败

时间:2019-02-21 16:30:01

标签: web-scraping lua scrapy scrapy-splash

我正在尝试使用以下lua脚本以刮擦飞溅的方式抓取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脚本集成的帮助吗?除了官方网站?

谢谢!

1 个答案:

答案 0 :(得分:0)

这看起来只是一个计时问题。我运行了您的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