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