Kosmonavt
Kosmonavt

Reputation: 191

read html page in Lua

I want to read this page using Lua https://smart-lab.ru/dividends/index/order_by_t2_date/desc/

I can do it with python. It reads all I want:

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

But I cannot do it with 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)

It prints only this: enter image description here

How can I download this page in Lua? Not duplicate of this.

because my request doesn't reurn nor 301 nor 302

Upvotes: 0

Views: 1288

Answers (2)

gview
gview

Reputation: 15411

Using luarocks you install luasec:

luarocks install luasec

This will then allow you to require ssl.https

luasec depends on having Openssl development package installed on your system. The way to do this depends greatly on your OS.

Upvotes: 0

Mike V.
Mike V.

Reputation: 2215

for https links, you need to use ssl library, try this code:

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) )

Upvotes: 2

Related Questions