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         }
  • 相关阅读:
    从表达式到变量:一行scheme代码之所见
    document 文挡对象详解(JavaScript脚本语言描述)
    开源Linux系统成为微软Vista竞争对手
    Java下的框架编程(5)cglib的应用
    如何做好职业规划(乾卦)
    Asianux将成为最先进的安全Linux操作系统
    开发人员行走Unix的随身四艺
    Java中类初始化的顺序
    用Java线程获取优异性能(II)——使用同步连载线程访问关键代码部份
    prototype.js的扩展
  • 原文地址:https://www.cnblogs.com/angleSJW/p/2093106.html
Copyright © 2011-2022 走看看