Lua : Fetch a webpage

匆匆过客 提交于 2019-12-08 18:48:40

问题


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.


回答1:


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:

local http=require'socket.http'
local body, statusCode, headers, statusText = http.request('http://w3.impa.br/~diego/software/luasocket/http.html')
print('statusCode ', statusCode)
print('statusText ', statusText)
print('headers ')
for index,value in pairs(headers) do
    print("\t",index, value)
end
print('body',body)



回答2:


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.




回答3:


if you don't have socket (like me), but you have the http library/module, then you could try this:

http.get("https://nodemcu.readthedocs.io/en/master/en/modules/http/", nil, function(code, data)
    if (code ~= 200) then
        print("HTTP request failed")
    else
        print(code, data)
    end
end)

it works for me

you can find more info in the docs https://nodemcu.readthedocs.io/en/master/en/modules/http/#httpget



来源:https://stackoverflow.com/questions/6858500/lua-fetch-a-webpage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!