zoukankan      html  css  js  c++  java
  • 获取Html页面元素属性内容

    获取网页HTML元素内容方法
    ①通过 正则表达式 匹配获取

                  

    View Code
     1 string resquestUrl = string.Empty;
     2                 //过来HTML元素方法
     3                 Regex rxGetInfo = new Regex("<label for=\"caller\">.*?</label>", RegexOptions.IgnoreCase);
     4                 Regex rxFilter = new Regex("<.*?>");
     5                 HttpWebRequest request = WebRequest.Create(resquestUrl) as HttpWebRequest;
     6                 HttpWebResponse response = request.GetResponse() as HttpWebResponse;
     7                 StreamReader sr = new StreamReader(response.GetResponseStream());
     8                 string returnContent = sr.ReadToEnd();
     9                 sr.Close();
    10                 response.Close();
    11                 MatchCollection mc = rxGetInfo.Matches(returnContent);


    ②根据元素属性 GetElementById获取
     HtmlDocument temphtml = new HtmlDocument();
     temphtml.GetElementById();
    ③过滤html标签

    View Code
     1  /// <summary>
     2         /// 过滤html标签
     3         /// </summary>
     4         /// <param name="strHtml">html的内容</param>
     5         /// <returns></returns>
     6         public static string StripHTML(string stringToStrip)
     7         {
     8             // paring using RegEx           //
     9             stringToStrip = Regex.Replace(stringToStrip, "</p(?:\\s*)>(?:\\s*)<p(?:\\s*)>""\n\n", RegexOptions.IgnoreCase | RegexOptions.Compiled);
    10             stringToStrip = Regex.Replace(stringToStrip, "<br(?:\\s*)/>""\n", RegexOptions.IgnoreCase | RegexOptions.Compiled);
    11             stringToStrip = Regex.Replace(stringToStrip, "\"""''", RegexOptions.IgnoreCase | RegexOptions.Compiled);
    12             stringToStrip = StripHtmlXmlTags(stringToStrip);
    13             return stringToStrip;
    14         }
    15 
    16         private static string StripHtmlXmlTags(string content)
    17         {
    18             return Regex.Replace(content, "<[^>]+>""", RegexOptions.IgnoreCase | RegexOptions.Compiled);
    19         }
  • 相关阅读:
    MySQL-8.0.18生成随机密码特性
    Oracle-19C新特性-自动清理网络日志文件
    Oracle-管理Data Guard Standby Database
    页面部分文字模糊问题修复
    C语言 指针数组
    C语言 指针和数组区别
    C语言 使用指针遍历数组
    阿里云服务器(ECS)在Ubuntu 18.04安装Docker
    Python总结之处理时间(time)、日期时间(datetime)、日历(calendar)
    终生制:疯狂创客圈 JAVA 架构班(又名 疯狂创客圈社群VIP)
  • 原文地址:https://www.cnblogs.com/angleSJW/p/2093106.html
Copyright © 2011-2022 走看看