Web scraping Lua脚本单击按钮失败
我正在尝试使用以下lua脚本使用scrapy splash从中刮取航班:Web scraping Lua脚本单击按钮失败,web-scraping,lua,scrapy,scrapy-splash,Web Scraping,Lua,Scrapy,Scrapy Splash,我正在尝试使用以下lua脚本使用scrapy splash从中刮取航班: function main(splash) local waiting_time = 2 -- Go to the URL assert(splash:go(splash.args.url)) splash:wait(waiting_time) -- Clic
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'}
有人知道为什么会这样吗?
还有谁知道我在哪里可以得到与splash集成的toturial for lua脚本?除了官方网站
提前谢谢 这看起来只是个时间问题。我运行了几次你的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
splash:select('ctl00\u rpt…
返回了nil
而不是预期的按钮对象。@谢谢您的回答!我使用jQuery在chrome上进行了检查,如:$('ctl00\u rptoughtingflights\u divPaging>div.advanced.noTop>a')。单击()这是有效的。知道lua脚本为什么找不到元素吗?我更改了代码,仍然得到nil元素的这个错误,我还尝试了while循环条件(element==nil),但没有帮助。有什么想法吗?也许这里有些东西是异步的,需要使它同步?谢谢