0

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

Kosmonavt
  • 191
  • 2
  • 14
  • Possible duplicate of [luaSocket HTTP requests always respond with a redirect (301 or 302)](https://stackoverflow.com/questions/37555226/luasocket-http-requests-always-respond-with-a-redirect-301-or-302) – gview Mar 14 '19 at 17:50
  • @Carcigenicate actually the default behavior of the library already follows 301's. The issue is that the request is for https:.. and this causes an internal loop because a different library is required to handle https. – gview Mar 14 '19 at 17:52
  • @Carcigenicate No worries, you were just trying to help, and I appreciate that effort – gview Mar 14 '19 at 18:00
  • I highly recommend to use [Lua-cURL](https://github.com/Lua-cURL/Lua-cURLv3) for HTTP requests. Lua socket combined with luasec makes problems with actual HTTP- and TLS-versions. – csaar Mar 15 '19 at 08:15
  • You could also shell out to `curl`, which is provided by most Windows and Linux distributions. – Tom Blodget Mar 15 '19 at 16:50

2 Answers2

2

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) )
Mike V.
  • 2,077
  • 8
  • 19
  • Please help me to install this library. luarocks doesn't help. Error: Could not find expected file openssl/ssl.h for OPENSSL -- you may have to install OPENSSL in your system and/or set the OPENSSL_DIR variable. I found library here "https://github.com/brunoos/luasec" but I don't know how to install it. – Kosmonavt Mar 14 '19 at 20:52
  • see answer below – Mike V. Mar 15 '19 at 09:07
0

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.

gview
  • 14,876
  • 3
  • 46
  • 51