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;
        }
    
    
    
    
    
  • 相关阅读:
    [CF590C] Three States
    [CF767B] The Queue
    [CF1296F] Berland Beauty
    [CF3D] Least Cost Bracket Sequence
    YUV420 转 RGB 测试
    [POI2012] TOU-Tour de Byteotia
    [CF576C] Points on Plane
    [CF191C] Fools and Roads
    [CF1485C] Floor and Mod
    [CF1399D] Binary String To Subsequences
  • 原文地址:https://www.cnblogs.com/KingUp/p/4328962.html
Copyright © 2011-2022 走看看