本帖最后由 zmoli775 于 2023-12-14 13:00 编辑
获取语雀代码块内容(干货)// 有图有真相
代码开始
[JavaScript] 纯文本查看 复制代码 void Main()
{
yuque_int775Async();
}
public static async Task yuque_int775Async()
{
string[] ds = {
"int775", // 用户名
"s1", // 知识库ID
"pur0srinpwwlyxo2", // 文档ID
"https://www.yuque.com/api/v2/", // 语雀API地址
"repos", "docs", "data", // API缺省值
"body_draft", "<code>", "</code>", // 取值关键字
"X-Auth-Token",
"i9FwjM*************************fvILpFB0h", // 令牌
"User-Agent", "curl/8.4.0" // UserAgent标识
};
try
{
using HttpClient hc = new() { Timeout = new TimeSpan(0, 0, 0, 0, 800), BaseAddress = new Uri(ds[3]) };
Dictionary<string, string> header = new() { { ds[10], ds[11] }, { ds[12], ds[13] } };
foreach (var kv in header) { hc.DefaultRequestHeaders.Add(kv.Key, kv.Value); }
string res = await hc.GetStringAsync($"{ds[4]}/{ds[0]}/{ds[1]}/{ds[5]}/{ds[2]}");
// Console.WriteLine(res);
using JsonDocument jd = JsonDocument.Parse(res);
JsonElement je = jd.RootElement.GetProperty(ds[6]).GetProperty(ds[7]);
string? js = je.GetString();
if (!string.IsNullOrEmpty(js))
{
Match match = Regex.Match(js, $"(?<=({ds[8]}))[.\\s\\S]*?(?=({ds[9]}))", RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.Singleline);
js = match.Success ? match.Value : string.Empty;
Console.WriteLine(js);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
代码结束
code.txt
(1.64 KB, 下载次数: 4)
|