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(); }