C#抖音地址解析获取水印地址

源码下载:DEMO下载

首先我们可以看到手机上的APP播放的视频是没有水印的,如下:

这样我们可以用Charles之类的工具抓取该地址,主要观看他的前缀地址部分(由于配置比较麻烦,大家可以自行配置,参看https://www.jianshu.com/p/c422344798c5,手机上的应用都可以抓取)

会发现播放地址是这样的:https://aweme.snssdk.com/aweme/v1/play/........

第二步:点击分享,复制地址,得到链接 https://v.douyin.com/7SUFkb/

第三步:我们使用谷歌浏览器访问(方便调试)

第四步:我们右键查看源码,会发现下图的地址

 

第五步:我们切换浏览器为手机模式,然后在看源码变化(我们会拿到第一步播放地址的关键部分参数),这一步只是为了验证PC浏览器模式和手机浏览模式的源码是不一样的。

第六步:在一次验证,我们访问第四步拿到地址(https://aweme.snssdk.com/aweme/v1/playwm/?s_vid=93f1b41336a8b7a442dbf1c29c6bbc562f1d6ac08e2f68576e6a34ac31a6790e5502ca4913dc07a83eabc3d41484e8ac7f43878815a424e99ceca999c77d5265&line=0),是可以得到视频源的,但是有水印,如下:

第七步:对比第一步(手机)和第四步(PC)拿到的地址,发现其地址playwm和play的变化,然后替换一下访问 https://aweme.snssdk.com/aweme/v1/play/?s_vid=93f1b41336a8b7a442dbf1c29c6bbc562f1d6ac08e2f68576e6a34ac31a6790e5502ca4913dc07a83eabc3d41484e8ac7f43878815a424e99ceca999c77d5265&line=0

得到无水印地址(提示:经验证这里得用浏览器的手机模式,pc模式无法访问

 

经过上面几步,从第二步开始,抖音地址就经过了几次重定向才拿到真实的数据源地址

下面是C#实现的代码:

 /// <summary>
        /// 抖音
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public string DouYin(string url)
        {
            var webHtml = VideoHelper.GetWebContent(url, 0);
            var webArr1 = Regex.Split(webHtml, "playAddr: \"", RegexOptions.IgnoreCase);
            var webArr2 = Regex.Split(webArr1[1], "\",", RegexOptions.IgnoreCase);
            var playAddr = webArr2[0].Replace("playwm", "play");
            var payUrl = VideoHelper.GetMapUrl(playAddr, 1);
            return payUrl;
        }
/// <summary>
        /// 获取页面html
        /// </summary>
        /// <param name="Url">页面路径</param>
        /// <param name="type">浏览器类型(0PC/1手机)</param>
        /// <returns></returns>
        public static string GetWebContent(string Url, int type = 0)
        {
            string strResult = "";
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
                //声明一个HttpWebRequest请求 
                request.Method = "POST";
                var UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.861.0 Safari/535.2";
                if (type > 0)
                {//模拟手机浏览器
                    UserAgent = "Mozilla/5.0 (Linux; U; Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1";
                }
                request.UserAgent = UserAgent;

                request.ContentType = "application/x-www-form-urlencoded";
                request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8"));
                strResult = sr.ReadToEnd();
                response.Close();
            }
            catch
            {
                Console.WriteLine("出错");
            }
            return strResult;
        }


        /// <summary>
        /// 获取重定向地址
        /// </summary>
        /// <param name="Url">原地址</param>
        /// <param name="type">浏览器类型(0PC/1手机)</param>
        /// <returns></returns>
        public static string GetMapUrl(string Url, int type = 0)
        {
            string strResult = "";
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
                request.Method = "POST";
                var UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.861.0 Safari/535.2";
                if (type > 0)
                {//模拟手机浏览器
                    UserAgent = "Mozilla/5.0 (Linux; U; Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1";
                }
                request.UserAgent = UserAgent;
                request.ContentType = "application/x-www-form-urlencoded";
                request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
                using (WebResponse response = request.GetResponse())
                {
                    strResult = response.Headers["Location"];      //重定向地址
                }


            }
            catch
            {
                Console.WriteLine("出错");
            }
            return strResult;
        }

源码下载:DEMO下载

posted @ 2020-03-24 18:14  落日知暮  阅读(3144)  评论(1编辑  收藏  举报
作者:boyzi007 出处:http://www.cnblogs.com/boyzi/ QQ:470797533 QQ交流群:364307742 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。