gpt4 book ai didi

lua - 在 Lua 中下载文件的最简单方法

转载 作者:行者123 更新时间:2023-12-02 04:55:59 28 4
gpt4 key购买 nike

我的 Lua 应用程序发出 http: 请求,获得 JSON 格式的响应,将该 JSON 解码为表格。好的。

现在,我可以提取该 JSON 文件中的 url。

但是,如何在 Lua 中将图像 url 保存到文件中?有可用的图书馆吗?

编辑:

我如何在 Lua 中获取 JSON 文件:

json_lib = require('json')

local http = libs.net.http();

local resp = http:request({
method = "get",
url = "http://developer.echonest.com/api/v4/artist/images?api_key=MY_API_KEY_HERE&name=zedd&format=json&results=1&start=0&license=unknown",
});


local json_full = resp.content;

local str_decoded = json_lib.decode(json_full)

function RecursiveSearch(aTable)

for key, value in pairs(aTable) do --unordered search
if(type(value) == "table") then
print(key,value)
RecursiveSearch(value)
else
--Do something with this.
print(key,value)
end
end
end
RecursiveSearch(str_decoded)

我的 JSON 响应是:

{
"response":{
"status":{
"version":"4.2",
"code":0,
"message":"Success"
},
"start":0,
"total":20,
"images":[
{
"url":"http://userserve-ak.last.fm/serve/_/67419844/ZEDD.png",
"license":{
"type":"unknown",
"attribution":"n/a",
"url":"http://www.last.fm/music/ZEDD/+images"
}
}
]
}
}

所以我想在磁盘上保存带有艺术家图片的第一个 URL。

最佳答案

添加类似(未经测试)的内容:

local imageresp = http:request({
method = "get",
url = url_from_decoded_json,
});

local imagefile = io.open("some_file", "w")
imagefile:write(resp.content)
imagefile:close()

到脚本的末尾。

关于lua - 在 Lua 中下载文件的最简单方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17959425/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com