如何使用Luacurl / Libcurl / Curl和Lua获取HTML代码

我的代码中缺少什么能够获取网站的源代码(感谢@MichalKottman)?

与在Chrome中右键单击并单击“查看页面源代码”一样。

local curl = require "luacurl"
local c = curl.new()

function GET(url)
    c:setopt(curl.OPT_URL,url)
    c:setopt(curl.OPT_PROXY,“http://myproxy.bla.com:8080”)
    c:setopt(curl.OPT_HTTPHEADER,“Connection:Keep-Alive”,“Accept-Language:en-us”)
    c:setopt(curl.OPT_CONNECTTIMEOUT,30)
    local t = {} - 这将收集结果块
    c:setopt(curl.OPT_WRITEFUNCTION,function(param,buf)
        table.insert(t,buf)-存储接收到的数据块
        return #buf
    end)
    c:setopt(curl.OPT_PROGRESSFUNCTION,function(param,dltotal,dlnow)
        print'%',url,dltotal,dlnow)-在此处进行您的花式报告
    end)
    c:setopt(curl.OPT_NOPROGRESS,false)-使用此选项来激活进度
    assert(c:perform())
    return table.concat(t) - 将整个数据作为字符串返回
end

--local s = GET 'http://www.lua.org/'
local s = GET 'https://www.youtube.com/watch?v=dT_fkwX4fRM'
print(s)
file = io.open(“text.html”,“wb”)
file:write(s)
file:close()

很不幸,必须使用Lua和使用luacurl绑定libcurl,因为提供代理时luasocket不能工作(至少对我来说是这样)。 我的下载文件为空。使用cmd我可以毫不费力地获取页面源代码 curl http://mypage.com

对于lua.org,它能够完美地工作,但对于YouTube链接则不能。我错过了什么?

点赞
用户4096653
用户4096653
local curl = require "luacurl"
local c = curl.new()

function GET(url)
    c:setopt(curl.OPT_URL, url)
    c:setopt(curl.OPT_PROXY, "http://myproxy.com:8080")
    c:setopt(curl.OPT_HTTPHEADER, "Connection: Keep-Alive", "Accept-Language: en-us")
    c:setopt(curl.OPT_CONNECTTIMEOUT, 30 )
    c:setopt(curl.OPT_FOLLOWLOCATION, true) -- REALLY IMPORTANT ELSE FAIL
    c:setopt(curl.OPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36")
    c:setopt(curl.OPT_SSL_VERIFYPEER, false) -- REALLY IMPORTANT ELSE NOTHING HAPPENS -.-
    c:setopt(curl.OPT_ENCODING, "utf8") -- could be important
    local t = {} -- this will collect resulting chunks
    c:setopt(curl.OPT_WRITEFUNCTION, function (param, buf)
        table.insert(t, buf) -- store a chunk of data received
        return #buf
    end)
    c:setopt(curl.OPT_PROGRESSFUNCTION, function(param, dltotal, dlnow)
        print('%', url, dltotal, dlnow) -- do your fancy reporting here
    end)
    c:setopt(curl.OPT_NOPROGRESS, false) -- use this to activate progress
    assert(c:perform())
    return table.concat(t) -- return the whole data as a string
end
2015-04-24 09:44:53