Lua:获取网页
我想获取一个网页并以字符串形式获取结果,但我不知道该怎么做。我在网上搜索并没有找到如何操作。
I want to fetch a webpage and get the result in a string, but I don't know how to do it. I search online and didn't find how to do it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我只需使用 Lua Socket ,它带有 http 子模块。您可以简单地使用 http.request 将网页放入您想要的任何容器中(默认为字符串,但您可以使用表、文件、stdio...使用 ltn12 过滤器和接收器)。
举个例子:
I'd simply use Lua Socket which comes with an http submodule. You can simply use http.request to get a webpage into whatever container you'd want (default is string, but you can use a table, a file, stdio, ... using ltn12 filters and sinks).
As an example:
如果您找不到确切的 http 客户端库,您可以自己实现,或者构建 别人的工作。
在该链接中,虽然它被称为 libhttpd,但作者明确指出它可以用于任何用途。看起来像是一个更可用的 lua 套接字包装器。
If you can't find an exact http client library, you could implement on yourself, or build on someone else's work.
In that link, although it is called libhttpd, but the author clearly states that it can be used for anything. Looks like a more usable wrapper around lua sockets.
如果你没有套接字(像我一样),但你有 http 库/模块,那么你可以尝试这个:
它对我有用,
你可以在文档中找到更多信息 https://nodemcu.readthedocs.io/en/master/en/modules/http/#httpget
if you don't have socket (like me), but you have the http library/module, then you could try this:
it works for me
you can find more info in the docs https://nodemcu.readthedocs.io/en/master/en/modules/http/#httpget