zoukankan      html  css  js  c++  java
  • C#对HTML文档的解析

    相信很多人都有过HTML文档解析的需求。比如我们抓取了某1个网站的页面数据,格式就是HTML的格式。以前我们都是通过正则表达式来进行解析,但是发现有一些问题。解析HTML文档时并不容易,如果文档的格式稍有变化很可能就不能正确的匹配。因此我们需要专门的工具来帮助我们轻松的解析HTML文档。

    其实已经有一个非常不错的工具提供了。比如HtmlAgilityPack。它可以帮助我们解析HTML文档就像用XmlDocument类来解析XML一样轻松、方便。

    这个工具可以在http://htmlagilitypack.codeplex.com/下载到,里面有支持各种.NET Framework的版本的dll。

    好了,下面提供一个足够Simple的例子给大家。大家可以在此基础之上,举一反三。

    比如要解析下面的HTML。

    <table>
        <thead>
            <tr>
                <th>时间</th>
                <th>类型</th>
                <th>名称</th>
                <th>单位</th>
                <th>金额</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>2013-12-29</td>
                <td>发票1</td>
                <td>采购物资发票1</td>
                <td>某某公司1</td>
                <td>123元</td>
            </tr>
            <tr>
                <td>2013-12-29</td>
                <td>发票2</td>
                <td>采购物资发票2</td>
                <td>某某公司2</td>
                <td>321元</td>
            </tr>
        <tbody>
    </table>

    以控制台项目为例,首先要引用HtmlAgilityPack.dll文件,这样才能使用dll里面的类和方法。

            static void Main(string[] args)
            {
                string strWebContent = @"<table><thead>
                <tr>
                  <th>时间</th>
                  <th>类型</th>
                  <th>名称</th>
                  <th>单位</th>
                  <th>金额</th>
                </tr>
                </thead>
                <tbody>" +
                @"<tr>
                  <td>2013-12-29</td>
                  <td>发票1</td>
                  <td>采购物资发票1</td>
                  <td>某某公司1</td>
                  <td>123元</td>
                </tr>" +
                @"<tr>
                  <td>2013-12-29</td>
                  <td>发票2</td>
                  <td>采购物资发票2</td>
                  <td>某某公司2</td>
                  <td>321元</td>
                </tr>
                </tbody>
              </table>
            ";
    
                List<Data> datas = new List<Data>();//定义1个列表用于保存结果
    
                HtmlDocument htmlDocument = new HtmlDocument();
                htmlDocument.LoadHtml(strWebContent);//加载HTML字符串,如果是文件可以用htmlDocument.Load方法加载
    
                HtmlNodeCollection collection = htmlDocument.DocumentNode.SelectSingleNode("table/tbody").ChildNodes;//跟Xpath一样,轻松的定位到相应节点下
                foreach (HtmlNode node in collection)
                {
                    //去除
    以及空格,获取到相应td里面的数据
                    string[] line = node.InnerText.Split(new char[] { '
    ', '
    ', ' ' }, StringSplitOptions.RemoveEmptyEntries);
    
                    //如果符合条件,就加载到对象列表里面
                    if (line.Length == 5)
                        datas.Add(new Data() { 时间 = line[0], 类型 = line[1], 名称 = line[2], 单位 = line[3], 金额 = line[4] });
                }
    
                //循环输出查看结果是否正确
                foreach (var v in datas)
                {
                    Console.WriteLine(string.Join(",", v.时间, v.类型, v.名称, v.单位, v.金额));
                }
            }
        /// <summary>
        /// 定义的实体类用于接收数据
        /// </summary>
        public class Data
        {
            public string 时间 { get; set; }
            public string 类型 { get; set; }
            public string 名称 { get; set; }
            public string 单位 { get; set; }
            public string 金额 { get; set; }
        }

    上面就是完整的代码,注释也很清楚。

    最后看一下解析的结果:

  • 相关阅读:
    Path Sum II
    Convert Sorted Array to Binary Search Tree
    Construct Binary Tree from Inorder and Postorder Traversal
    Construct Binary Tree from Preorder and Inorder Traversal
    Maximum Depth of Binary Tree
    Binary Tree Zigzag Level Order Traversal
    Binary Tree Level Order Traversal
    Same Tree
    Validate Binary Search Tree
    Binary Tree Inorder Traversal
  • 原文地址:https://www.cnblogs.com/guwei4037/p/4720182.html
Copyright © 2011-2022 走看看