吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 3120|回复: 7
收起左侧

[其他转载] C#爬取快手热门视频数据【萌新向】

[复制链接]
你与良方 发表于 2021-4-8 17:56
[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 {\n  type\n  author {\n    id\n    name\n    headerUrl\n    following\n    headerUrls {\n      url\n      __typename\n    }\n    __typename\n  }\n  photo {\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  }\n  canAddComment\n  llsid\n  status\n  currentPcursor\n  __typename\n}\n\nfragment photoResult on PhotoResult {\n  result\n  llsid\n  expTag\n  serverExpTag\n  pcursor\n  feeds {\n    ...feedContent\n    __typename\n  }\n  webPageArea\n  __typename\n}\n\nquery brilliantTypeDataQuery($pcursor: String, $hotChannelId: String, $page: String, $webPageArea: String) {\n  brilliantTypeData(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吾爱币 +1 热心值 +1 收起 理由
Antik1 + 1 + 1 用心讨论,共获提升!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

13169456869 发表于 2021-4-8 17:58
感谢分享!!学习学习!
dingqh 发表于 2021-4-8 18:14
sghzlx 发表于 2021-4-8 18:34
虾米来找茬 发表于 2021-4-8 18:52

谢谢,感谢分享
quandzhu 发表于 2021-4-16 09:04
很有帮助,共同学习
yjdsky 发表于 2021-4-16 10:46
不错,学习一下
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-25 18:58

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表