Lua中读取html页面

我想使用Lua来读取这个页面 https://smart-lab.ru/dividends/index/order_by_t2_date/desc/

我可以使用Python来实现它。 它可以读取我想要的所有内容:

from urllib.request import urlopen
txt=urlopen("https://smart-lab.ru/dividends/index/order_by_t2_date/desc/", timeout=10).readlines()
print(txt)

但是我不能使用Lua来做到这一点:

require "socket"
http = require 'socket.http'
local address = "https://smart-lab.ru/dividends/index/order_by_t2_date/desc/"
local body = http.request(address)

它只打印这个: enter image description here

我该如何在Lua中下载此页面? 不是重复此问题

因为我的请求既不返回301也不返回302。

点赞
用户7504558
用户7504558

对于 https 链接,你需要使用 ssl 库,尝试以下代码:

local https = require('ssl.https')
local url = 'https://smart-lab.ru/dividends/index/order_by_t2_date/desc/'
local resp = {}
local body, code, headers = https.request{ url = url,  sink = ltn12.sink.table(resp) }
if code~=200 then
    print("Error: ".. (code or '') )
    return
end
print("Status:", body and "OK" or "FAILED")
print("HTTP code:", code)
print("Response headers:")
if type(headers) == "table" then
  for k, v in pairs(headers) do
    print(k, ":", v)
  end
end
print( table.concat(resp) )
2019-03-14 19:55:53
用户860034
用户860034

使用 luarocks 安装 luasec

luarocks install luasec

然后你可以 require ssl.https

luasec 依赖于在你的系统中安装了 OpenSSL 开发包。如何安装取决于你的操作系统。

2019-03-14 21:14:37