C#爬取快手热门视频数据【萌新向】
using Flurl.Http;using Newtonsoft.Json.Linq;
using System;
using System.IO;
string vurl;
string filename;
string path = "D:\\Kuaishou\\";
string logpath = AppDomain.CurrentDomain.BaseDirectory + "log.txt";
string cookie = " ktrace-context=1|MS43NjQ1ODM2OTgyODY2OTgyLjc5MTI3NDE4LjE2MTc4NjYwOTUwNzYuMzE5Nzg=|MS43NjQ1ODM2OTgyODY2OTgyLjkyNDE3ODgyLjE2MTc4NjYwOTUwNzYuMzE5Nzk=|0|graphql-server|webservice|false|NA; clientid=3; did=web_cc45ac9b44e37e17f374f1831d455019; kpf=PC_WEB; kpn=KUAISHOU_VISION; didv=1617866151109";
string url = "https://video.kuaishou.com/graphql";
var data = new { operationName = "brilliantTypeDataQuery", variables = new { hotChannelId = "00", page = "brilliant", pcursor = "1" }, query = "fragment feedContent on Feed {\ntype\nauthor {\n id\n name\n headerUrl\n following\n headerUrls {\n url\n __typename\n }\n __typename\n}\nphoto {\n id\n duration\n caption\n likeCount\n realLikeCount\n coverUrl\n photoUrl\n coverUrls {\n url\n __typename\n }\n timestamp\n expTag\n animatedCoverUrl\n distance\n videoRatio\n liked\n stereoType\n __typename\n}\ncanAddComment\nllsid\nstatus\ncurrentPcursor\n__typename\n}\n\nfragment photoResult on PhotoResult {\nresult\nllsid\nexpTag\nserverExpTag\npcursor\nfeeds {\n ...feedContent\n __typename\n}\nwebPageArea\n__typename\n}\n\nquery brilliantTypeDataQuery($pcursor: String, $hotChannelId: String, $page: String, $webPageArea: String) {\nbrilliantTypeData(pcursor: $pcursor, hotChannelId: $hotChannelId, page: $page, webPageArea: $webPageArea) {\n ...photoResult\n __typename\n}\n}\n" };
var result = await url.WithHeaders(new
{
Connection = " keep-alive",
accept = " */*",
User_Agent = " Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36",
content_type = "application/json",
Accept_Language = "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
Cookie = cookie
})
.PostJsonAsync(data).ReceiveJson<JObject>();
var obj = result["data"]["brilliantTypeData"]["feeds"];
foreach (var o in obj)
{
vurl = o["photo"]["photoUrl"].ToString();
filename = o["author"]["name"] + "_" + o["photo"]["caption"] + ".mp4";
filename = filename.Replace("\\", "");
if (File.Exists(path + filename))
{
Console.WriteLine($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}]{filename} 文件已存在");
}
else
{
try
{
await vurl.DownloadFileAsync(path, filename);
Console.WriteLine($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}]{filename} 文件下载完成");
}
catch (Exception ex)
{
Console.WriteLine($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}]{filename} 文件下载失败,请查看日志");
File.AppendAllText(logpath, $"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}]{filename},{vurl},{ex.Message}\r\n");
}
}
}
Console.WriteLine();
Console.WriteLine($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] 全部下载完成");
Console.ReadLine();
有个bug没改,留给后人去改吧,看了一位盆友用Python的例子有感而发,地址:https://www.52pojie.cn/thread-1412847-1-1.html
共同进步吧 感谢分享!!学习学习! 学习学习 不错,学习一下
谢谢,感谢分享
很有帮助,共同学习 不错,学习一下
页:
[1]