zoukankan      html  css  js  c++  java
  • C#解析HTML源码

    刚做了一个小任务,需要抓取其他网站的部分数据,这里就顺便介绍使用Winista.Text.HtmlParser这个类库如何解析HTML并抓取部分数据

    1、获取指定网站的页面源码

    string url = "http://www.100njz.com/price/list/p--------1.html";
    System.Net.WebClient aWebClient = new System.Net.WebClient();
    aWebClient.Encoding = System.Text.Encoding.Default;
    string html = aWebClient.DownloadString(url);

    2、获取到源码后,解析并获取指定的节点数据,这里演示的是获取id="articleList"的div

    Lexer lexer = new Lexer(html);
            Parser parser = new Parser(lexer);
            NodeFilter filter = new NodeClassFilter(typeof(Winista.Text.HtmlParser.Tags.Div));
            NodeList nodeList = parser.Parse(filter);
            ITag t;
            if (nodeList.Count == 0)
                Response.Write("没有符合要求的节点");
            else
            {
                for (int i = 0; i < nodeList.Count; i++)
                {
                    t = getTag(nodeList[i]);
                    if (t != null && t.GetAttribute("id") == "articleList")
                    {
                        NodeFilter filter2 = new NodeClassFilter(typeof(Winista.Text.HtmlParser.Tags.LinkTag));
    
                        Response.Write(nodeList[i].ToHtml());
    
                    }
    
                }
            }
    private ITag getTag(INode node)
        {
            if (node == null)
                return null;
            return node is Div ? node as Div : null;
        }
    
    
    
    
    
  • 相关阅读:
    ThingJS之二十六问
    物联网开发,thingjs让您事半功倍!
    thingjs在线开发平台介绍
    jQuery· CSS样式方法
    jQuery属性
    jQuery效果
    JS事件委托中同一个标签执行不同操作
    js+php+mysql实现的学生成绩管理系统
    函数防抖
    两数之和
  • 原文地址:https://www.cnblogs.com/KingUp/p/4328962.html
Copyright © 2011-2022 走看看