C#获取html标签内容的方法


C# 获取html标签内容的方法:

        /// <summary>
        /// 获取html网页标签内容
        /// 例如:<span class="index_infoItem__ESU0o"></span>
        /// </summary>
        /// <param name="html">html内容</param>
        /// <param name="tag">标签 例如:span</param>
        /// <param name="attribute">标签属性 例如:class</param>
        /// <param name="value">标签属性值 例如:index_infoItem__ESU0o</param>
        /// <returns></returns>
        public static string[] RegexHtmlToFormat(string html, string tag, string attribute, string value)
        {
            List<string> list = new List<string>();
            string regex_html = @"<"+ tag + ".*?"+ attribute + "=.*?"+ value + ".*?[^>]*?>.*?</" + tag + ">"; //定义html标签的正则表达式 
            Regex regex = new Regex(regex_html, RegexOptions.IgnoreCase);
            if (regex.IsMatch(html))
            {
                MatchCollection matchCollection = regex.Matches(html);
                foreach (Match match in matchCollection)
                {
                    var valueHtml = match.Value;
                    valueHtml = Regex.Replace(valueHtml, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);//去除html标签
                    list.Add(valueHtml);//获取到的
                }
            }
            return list.ToArray();
        }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM