使用Lua脚本在Splash中访问google.com的DOM
2020-10-11 17:23:25
收藏:0
阅读:117
评论:2
我正在尝试在Splash中运行Lua脚本执行Google搜索,并获取搜索结果的截屏。当我尝试使用xpath或css选择器选择Google搜索框时,我的Lua脚本会发生如下错误:
{
"error": 400,
"type": "ScriptError",
"description": "在执行Lua脚本时发生错误",
"info": {
"message": "[string \"function main(splash, args)\r...\"]:9: 不能选择指定的元素 {'type': 'JS_ERROR', 'js_error_type': '语法错误', 'js_error_message': 'SyntaxError: DOM Exception 12', 'js_error': '错误:SyntaxError: DOM Exception 12', 'message': \"JS error: 'Error: SyntaxError: DOM Exception 12'\"}",
"type": "SPLASH_LUA_ERROR",
"splash_method": "select",
"source": "[string \"function main(splash, args)\r...\"]",
"line_number": 9,
"error": "不能选择指定的元素 {'type': 'JS_ERROR', 'js_error_type': 'SyntaxError', 'js_error_message': 'SyntaxError: DOM Exception 12', 'js_error': 'Error: SyntaxError: DOM Exception 12', 'message': \"JS error: 'Error: SyntaxError: DOM Exception 12'\"}"
}
}
这是我的Lua脚本:
function main(splash, args)
splash.private_mode_enabled = false
splash:set_user_agent("Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:81.0) Gecko/20100101 Firefox/81.0")
assert(splash:go(args.url))
assert(splash:wait(1.0))
search_box = assert(splash:select("//div[@class='a4bIc']/input"))
search_box:focus()
search_box:send_text('my user agent')
search_box:send_keys('<Enter>')
assert(splash:wait(2.0))
return splash:png()
end
我尝试设置自定义标头,以隐私模式运行脚本,但什么都没用。 然而,当使用duckduckgo.com时,相同的脚本可以正常运行并得出正确的输出。当目标URL是google.com时,问题就出现了。 我认为Google检测到浏览器正在被一个机器人(脚本)控制,因此禁止访问DOM树。 有什么办法可以解决这个问题吗?
点赞
用户3342050
也许页面还没有完全下载/渲染
function main(splash, args)
splash.private_mode_enabled = false
splash:set_user_agent("Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:81.0) Gecko/20100101 Firefox/81.0")
local ok, reason = assert( splash:go(args.url) )
if ok then
local wait, increment, maxwait = 0, 0.1, 10
while wait < maxwait and not splash:select("//div[@class='a4bIc']/input") do
splash:wait(increment) -- 等待直到它存在,或者超时
wait = wait +increment
end
if wait >= maxwait then
print('超时')
else
search_box = splash:select("//div[@class='a4bIc']/input")
search_box:focus()
search_box:send_text('my user agent')
search_box:send_keys('<Enter>')
splash:wait(2.0)
return splash:png()
end
else
print( reason ) -- 查看是否有告诉你为什么
end
end
2020-10-22 19:33:32
评论区的留言会收到邮件通知哦~
推荐文章
- Lua 虚拟机加密load(string.dump(function)) 后执行失败问题如何解决
- 我想创建一个 Nginx 规则,禁止访问
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?

你的选择器有问题。
"//div[@class='a4bIc']/input"打开网页,点击F12键,使用检查器确定要针对该输入字段进行定位的div类。也可能是它们的类名是动态生成的,以混淆它们。