【发布时间】:2022-01-25 06:46:16
【问题描述】:
我是 Lua 的新手,并尝试使用“for”循环遍历“items”中的所有链接,并使用 mouse_click() 函数单击所有链接。但它会抛出 "error": "attempt to call method 'mouse_click' (a nil value)" 我该怎么做?我自己尝试了下面提到的代码,但可能缺少一些东西。从文档中,我发现 Lua 有两种类型的 for 循环。数字和通用。但是要遍历链接,然后单击它我必须使用什么?
再一次,我对 Lua 很陌生。只是想熟悉它,所以我正在寻求所有专家的指导。
function main(splash, args)
splash:set_user_agent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36")
splash.private_mode_enabled = false
assert(splash:go(args.url))
assert(splash:wait(3))
items = assert(splash:select("div.item-list-content a"))
for item in pairs(items) do
item:mouse_click()
assert(splash:wait(5))
end
splash:set_viewport_full()
return {
html = splash:html(),
png = splash:png(),
har = splash:har(),
}
end
输出
{
"error": 400,
"type": "ScriptError",
"description": "Error happened while executing Lua script",
"info": {
"source": "[string \"function main(splash, args)\r...\"]",
"line_number": 11,
"error": "attempt to call method 'mouse_click' (a nil value)",
"type": "LUA_ERROR",
"message": "Lua error: [string \"function main(splash, args)\r...\"]:11: attempt to call method 'mouse_click' (a nil value)"
}
}
【问题讨论】:
-
哪里说有个函数叫
mouse_click? -
@JosephSible-ReinstateMonica 抱歉输入错误。这是一种方法
-
你有网站的链接吗?使用它检查您的代码并找到改进更容易。
标签: lua scrapy lua-table scrapy-splash