当前位置:首页 » 《关于电脑》 » 正文

c#解析抖音评论提取数据第一篇源码

28 人参与  2024年11月25日 18:01  分类 : 《关于电脑》  评论

点击全文阅读


这里给出接续视频对应数据,评论提取截取源码将在第二篇写入
解析视频名称: 最新解析标签
public string title_ceng(string html)

    {        string title_b = "";        string htmlContent = html;        try        {            // string title = "";//获取title值 标题 视频名称\            //arnSiSbK hT34TYMB ONzzdL2F            Regex regex = new Regex(@"<span class=""arnSiSbK hT34TYMB ONzzdL2F"">(.*?)</span>", RegexOptions.IgnoreCase);           // Regex regex = new Regex(@"<span class=""cx3p4vL2 NhsqQqNv BWrTO7Je"">(.*?)</span>", RegexOptions.IgnoreCase);            Match match = regex.Match(htmlContent);            if (match.Success)            {                // 获取匹配到的第一个组(即<title>和</title>之间的内容)                title_b = match.Groups[1].Value;                //发布时间:                title_b = title_b.Replace("<span>", "");                title_b = title_b.Replace("/", "");                string pattern = @"<[^>]*>";                title_b = Regex.Replace(title_b, pattern, string.Empty);            }        }        catch        {            //MessageBox.Show("608");        }        return title_b;    }

解析视频作者
public string zuozhe_ceng(string html)
{
string zuozhe = “”;

        string htmlContent = html;        try        {            // string title = "";//获取title值 标题 视频名称\            // Regex regex = new Regex(@"<div class=""account-name"">(.*?)</div>", RegexOptions.IgnoreCase);            //arnSiSbK ypGAC_xH ONzzdL2F                Regex regex = new Regex(@"<span class=""arnSiSbK ypGAC_xH ONzzdL2F"">(.*?)</span>", RegexOptions.IgnoreCase);            //Regex regex = new Regex(@"<span class=""cx3p4vL2 JO0TrMZd BWrTO7Je"">(.*?)</span>", RegexOptions.IgnoreCase);            Match match = regex.Match(htmlContent);            if (match.Success)            {                // 获取匹配到的第一个组(即<title>和</title>之间的内容)                zuozhe = match.Groups[1].Value;                //发布时间:                zuozhe = zuozhe.Replace("<span>", "");                zuozhe = zuozhe.Replace("/", "");            }        }        catch        {            //MessageBox.Show("608");        }        return zuozhe;    }//视频作者

解析视频时间
public string shipin_shijian_ceng(string html)
{
string shipin_dates = “”;
string htmlContent = html;
try
{
// string title = “”;//获取title值 标题 视频名称
Regex regex = new Regex(@“<span class=”“time”“>(.*?)”, RegexOptions.IgnoreCase);
Match match = regex.Match(htmlContent);
if (match.Success)
{
// 获取匹配到的第一个组(即和之间的内容)
shipin_dates = match.Groups[1].Value.Trim();
//发布时间:
shipin_dates = shipin_dates.Replace(“”, “”);
shipin_dates = shipin_dates.Replace(“/”, “”);
shipin_dates = shipin_dates.Replace(“·”, “”);

                shipin_dates = shipin_dates.Replace("日", "");                shipin_dates = shipin_dates.Replace("年", "-");                shipin_dates = shipin_dates.Replace("月", "-");                string day = "";                // try                // {                Regex yearRegex = new Regex(@"\b\d{4}\b");                Regex dateRegex = new Regex(@"\b\d{1,2}-\d{1,2}\b");                // 判断字符串中是否包含年份信息                if (yearRegex.IsMatch(shipin_dates.Trim()))                {                    // Console.WriteLine("输入字符串包含年份信息");                }                else if (dateRegex.IsMatch(shipin_dates.Trim()))                {                    // Console.WriteLine("输入字符串不包含年份信息,但包含日期信息");                    shipin_dates = "2024-" + shipin_dates.Trim();                }                else                {                    Console.WriteLine("输入字符串既没有年份信息,也不符合日期格式");                    #region                    //DateTime shipin_dates_y = Convert.ToDateTime(shipin_dates);                    //if (shipin_dates_y.Year != 1)                    //{                    //    shipin_dates = "2004-" + shipin_dates.Trim ();                    //    Console.WriteLine("这个日期变量包含年份。");                    //}                    //else                    //{                    //    shipin_dates = "2004-" + shipin_dates.Trim ();                    //    //Console.WriteLine("这个日期变量不包含年份。");                    //}                    #endregion                    //  }                    //  catch                    // {                    char delimiter = '·';                    int index1 = shipin_dates.IndexOf(delimiter);                    if (index1 != -1)                    {                        string textBeforeDelimiter = shipin_dates.Substring(0, index1);                        shipin_dates = textBeforeDelimiter;                        Console.WriteLine("Text before delimiter: " + textBeforeDelimiter);                    }                    if (shipin_dates.Contains("天"))                    {                        //  pinglun_riqi_yuanshi = extraInfo;                        int index = shipin_dates.IndexOf("天");                        day = shipin_dates.Substring(0, index);                        DateTime dt = DateTime.Now.Date.AddDays(-Convert.ToInt32(Convert.ToInt32(day)));                        shipin_dates = dt.ToShortDateString();                    }                    if (shipin_dates.Contains("月"))                    {                        //pinglun_riqi_yuanshi = extraInfo;                        int index = shipin_dates.IndexOf("月");                        day = shipin_dates.Substring(0, index);                        DateTime dt = DateTime.Now.Date.AddMonths(-Convert.ToInt32(Convert.ToInt32(day)));                        shipin_dates = dt.ToShortDateString();                    }                    if (shipin_dates.Contains("小时"))                    {                        // pinglun_riqi_yuanshi = extraInfo;                        int index = shipin_dates.IndexOf("小时");                        day = shipin_dates.Substring(0, index);                        DateTime dt = DateTime.Now.Date.AddHours(-Convert.ToInt32(Convert.ToInt32(day)));                        shipin_dates = dt.ToString();                    }                    if (shipin_dates.Contains("分钟"))                    {                        //pinglun_riqi_yuanshi = extraInfo;                        int index = shipin_dates.IndexOf("分钟");                        day = shipin_dates.Substring(0, index);                        DateTime dt = DateTime.Now.Date.AddMinutes(-Convert.ToInt32(Convert.ToInt32(day)));                        shipin_dates = dt.ToString();                    }                    if (shipin_dates.Contains("周"))                    {                        //  pinglun_riqi_yuanshi = extraInfo;                        int index = shipin_dates.IndexOf("周");                        day = shipin_dates.Substring(0, index);                        int week = (Convert.ToInt32(day) * 7);                        DateTime dt = DateTime.Now.Date.AddDays(-Convert.ToInt32(week));                        shipin_dates = dt.ToShortDateString();                    }                    if (shipin_dates.Contains("年"))                    {                        //  pinglun_riqi_yuanshi = extraInfo;                        int index = shipin_dates.IndexOf("年");                        day = shipin_dates.Substring(0, index);                        DateTime dt = DateTime.Now.AddYears(-Convert.ToInt32(Convert.ToInt32(day)));                        shipin_dates = dt.ToShortDateString();                    }                    //判断当前时间是否和视频时间 是否大于                    DateTime a = DateTime.Now; // 当前时间                    DateTime b = DateTime.ParseExact(shipin_dates, "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture);// 视频时间,假设为 2022-05-27                    TimeSpan interval = a - b; // 计算时间间隔                    if (Math.Abs(interval.TotalDays) <= 730) // 判断时间间隔是否小于等于两年//这个里面的值  通过字段获取                    {                        Console.WriteLine("视频时间和当前时间在两年内");                    }                    else                    {                        Console.WriteLine("视频时间和当前时间不在两年内");                    }                }            }        }        catch        {            //MessageBox.Show("608");        }        return shipin_dates;    }//视频时间

在这里插入图片描述


点击全文阅读


本文链接:http://m.zhangshiyu.com/post/191932.html

<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

关于我们 | 我要投稿 | 免责申明

Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1