Web scraping 如何让Lua点击;“加载按钮”;无限?
这是我第一次使用splash来刮网站。我需要告诉splash单击一个按钮,以便在浏览器上加载其他元素。这是无限的。然后我希望splash返回HTML代码,这样我就可以用我的spider将其删除。加载按钮没有href,因此无法使用分页。因此,我试图编写一个splash脚本来实现这一点。但是,当我使用splash运行脚本时,“btn”部分似乎在返回的HTML中不起任何作用(每次只返回第一页的HTML) 以下是我写的启动脚本:Web scraping 如何让Lua点击;“加载按钮”;无限?,web-scraping,lua,scrapy-splash,Web Scraping,Lua,Scrapy Splash,这是我第一次使用splash来刮网站。我需要告诉splash单击一个按钮,以便在浏览器上加载其他元素。这是无限的。然后我希望splash返回HTML代码,这样我就可以用我的spider将其删除。加载按钮没有href,因此无法使用分页。因此,我试图编写一个splash脚本来实现这一点。但是,当我使用splash运行脚本时,“btn”部分似乎在返回的HTML中不起任何作用(每次只返回第一页的HTML) 以下是我写的启动脚本: function main(splash,args) local
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:
非常感谢你
---编辑---
这是我在响应页面上得到的错误:
{
"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 '='"
}
}
如果不存在,请稍等片刻,然后重试。您可以对容器执行相同的操作,而不是
splash:wait(10)
。
等待程序可能是一个函数
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”部分,然后运行splash。它没有任何错误。但是,在回复页面上,我得到了网站第一页的HTML。它不应该显示整个HTML文件吗?或者它总是显示第一页?顺便说一句,“html:String(长度44838)”与以前一样。我对该网页了解不够,无法告诉您为什么会这样。也许页面需要cookies来导航。您可以尝试删除return语句中的花括号
return splash:html()
非常感谢,@Doyousketch2。我编辑了上面的文章,并在“编辑”部分输入了我得到的错误。正如你所说,我还更改了启动脚本。我知道这可能看起来很愚蠢,但我没有找到任何针对我遇到的错误的调试器,其他人的类似问题对这个错误没有帮助。所以,我把它放在这里。再次感谢你。
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()