zoukankan      html  css  js  c++  java
  • 正则获取 某段 DIV 中 的内容

    string html = "<div class='aa'><div class='left'>324324<div>dsfsdf</div><h1>aa</h1></div></div>";
                // 获取第一个 相呼应的标记
                //Regex reg = new Regex(@"<div class='left'>([sS]+?)</div>");
                // 获取前后对应的标记
                Regex reg = new Regex(@"(?is)<div class='left'[^>]*>(?><div[^>]*>(?<o>)|</div>(?<-o>)|(?:(?!</?div).)*)*(?(o)(?!))</div>");
                // 获取第一条
                Match first = reg.Match(html);
                // 获取匹配到的所有集合
                MatchCollection list = reg.Matches(html);
                foreach (Match item in list)
                {
                    string value = item.Value;
                }

            public static string GetElementByClassName(string htmlConetnt, string label, string className)
            {
                Regex reg = new Regex(string.Format(@"(?is)<{0} class=""{1}""[^>]*>(?><{0}[^>]*>(?<o>)|</{0}>(?<-o>)|(?:(?!</?{0}).)*)*(?(o)(?!))</{0}>", label, className));
                Match first = reg.Match(htmlConetnt);
                return first.Value;
            }
    
            public static string GetElementById(string htmlConetnt, string label, string id)
            {
                Regex reg = new Regex(string.Format(@"(?is)<{0} id=""{1}""[^>]*>(?><{0}[^>]*>(?<o>)|</{0}>(?<-o>)|(?:(?!</?{0}).)*)*(?(o)(?!))</{0}>", label, id));
                Match first = reg.Match(htmlConetnt);
                return first.Value;
            }
    
            public static string GetLabel(string htmlConetnt, string label)
            {
                Regex reg = new Regex(string.Format(@"(?is)<{0}[^>]*>(?><{0}[^>]*>(?<o>)|</{0}>(?<-o>)|(?:(?!</?{0}).)*)*(?(o)(?!))</{0}>", label));
                Match first = reg.Match(htmlConetnt);
                return first.Value;
            }
  • 相关阅读:
    浅谈Chrome V8引擎中的垃圾回收机制
    selenium反爬机制
    03 HTTP协议与HTTPS协议
    HTTP缓存机制和原理
    python 自动发送邮件
    02 Anaconda的介绍,安装记以及使用
    01 关于jupyter的环境安装
    SQLAlchemy
    django-debug-toolbar
    flask 第十篇 after_request before_request
  • 原文地址:https://www.cnblogs.com/liuxiaoji/p/5969006.html
Copyright © 2011-2022 走看看