Lua:获取网页 [英] Lua : Fetch a webpage

查看:34
本文介绍了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.

推荐答案

我会简单地使用 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).

举个例子:

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("	",index, value)
end
print('body',body)

这篇关于Lua:获取网页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆