lua抓取網路檔案
阿新 • • 發佈:2019-01-10
curl = require("luacurl") --下載這個頁面的.java檔案 downloadurl = "http://www.cs.princeton.edu/~rs/talks/LLRB/Java/" function get_html(url, c) local result = { } if c == nil then c = curl.new() end c:setopt(curl.OPT_URL, url) c:setopt(curl.OPT_WRITEDATA, result) c:setopt(curl.OPT_WRITEFUNCTION, function(tab, buffer) --call back函式,必須有 table.insert(tab, buffer) --tab引數即為result,參考http://luacurl.luaforge.net/ return #buffer end) local ok = c:perform() return ok, table.concat(result) --此table非上一個table,作用域不同 end ok, html = get_html(downloadurl) if ok then --分析頁面返回的.java的地址 for k in string.gfind(html, ">(%w+.java)<") do ok,javacode = get_html(downloadurl .. k) file = io.open("./"..k, "w"); if(file) then file:write(javacode); file:close(); end end else print ("Error" ) end