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         }
  • 相关阅读:
    关于docker remote api未授权访问漏洞的学习与研究
    c#调用 WinRAR.exe以命令行形式实现文件、文件夹的解压缩
    Redis作为多个Windows服务运行配置方法
    全景播放器-js+flash
    SQL 拼接多个字段的值&一个字段多条记录的拼接
    mysql-connector-java与mysql版本的对应
    Sass map详解
    情人节网站logo赏析
    表单标签动画畅想
    段落文字彩条效果
  • 原文地址:https://www.cnblogs.com/angleSJW/p/2093106.html
Copyright © 2011-2022 走看看