using System;
using HtmlAgilityPack;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections.Generic; namespace ConsoleApp
{
class Program
{
static string goText(HtmlNode _htmlnode, bool isSplit = true)
{
string str = ""; try {
// 获取text内容
str = _htmlnode.InnerText; // 消除多余的符号
str = Regex.Replace(str, "\r|\n|\t| ", "").Trim(); // 切割字符串
if (isSplit && str.IndexOf(":") >= ) {
str = str.Split(':')[];
}
}
catch { } return str;
} static void Main(string[] args)
{
// 获取index.html的内容
string basePath = AppDomain.CurrentDomain.BaseDirectory + "/index.html";
string html = "";
if (File.Exists(@basePath)) {
html = File.ReadAllText(@basePath, Encoding.Default);
} // 开始计算耗时
DateTime beforDT = System.DateTime.Now; // 使用HtmlAgilityPack解析它
var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(html); // 报告编号
var report_number = goText(htmlDoc.DocumentNode.SelectSingleNode("/html/body/div/div/table/tr[2]/td/table[1]/tbody/tr[2]/td[1]"));
// 查询时间
var query_time = goText(htmlDoc.DocumentNode.SelectSingleNode("/html/body/div/div/table/tr[2]/td/table[1]/tbody/tr[2]/td[2]"));
// 报告时间
var report_time = goText(htmlDoc.DocumentNode.SelectSingleNode("/html/body/div/div/table/tr[2]/td/table[1]/tbody/tr[2]/td[3]"));
// 姓名
var report_name = goText(htmlDoc.DocumentNode.SelectSingleNode("/html/body/div/div/table/tr[2]/td/table[2]/tbody/tr[1]/td[1]"));
// 证件类型
var report_type = goText(htmlDoc.DocumentNode.SelectSingleNode("/html/body/div/div/table/tr[2]/td/table[2]/tbody/tr[1]/td[2]"));
// 证件号码
var report_id = goText(htmlDoc.DocumentNode.SelectSingleNode("/html/body/div/div/table/tr[2]/td/table[2]/tbody/tr[1]/td[3]"));
// 婚姻
var report_marriage = goText(htmlDoc.DocumentNode.SelectSingleNode("/html/body/div/div/table/tr[2]/td/table[2]/tbody/tr[1]/td[4]")); // 表格
var table_tr = htmlDoc.DocumentNode.SelectNodes("/html/body/div/div/table/tr[2]/td/table[4]/tr[3]/td/table/tbody/tr/td/table/tbody/tr");
List<Table> list = new List<Table>();
// 遍历所有的tr
foreach (var node in table_tr) {
// 跳过第一次遍历吧
if (node.NodeType == HtmlNodeType.Element) {
// 获取所有的Td
var tds = node.Elements("td");
Table tb = new Table();
int i = ;
// 遍历所有的Td
foreach (var td in tds) {
if (td.NodeType == HtmlNodeType.Element) {
string text = goText(td, false);
// 使用比较蠢的方式赋值,自己想办法优化
switch (i)
{
case :
tb.a = text;
break;
case :
tb.b = text;
break;
case :
tb.c = text;
break;
case :
tb.d = text;
break;
}
}
i++;
}
list.Add(tb);
}
} // 删除第一个节点。我不需要表头
list.RemoveAt();
Console.Write(list); // 结算程序耗时
DateTime afterDT = System.DateTime.Now;
TimeSpan ts = afterDT.Subtract(beforDT);
Console.WriteLine("DateTime总共花费{0}ms.", ts.TotalMilliseconds);
Console.ReadLine();
}
} public class Table
{
/// <summary>
/// a
/// </summary>
public string a { get; set; }
/// <summary>
/// b
/// </summary>
public string b { get; set; }
/// <summary>
/// c
/// </summary>
public string c { get; set; }
/// <summary>
/// c
/// </summary>
public string d { get; set; }
}
}

C# Html Agility Pack的更多相关文章

  1. Html Agility Pack 解析Html

    Hello 好久不见 哈哈,今天给大家分享一个解析Html的类库 Html Agility Pack.这个适用于想获取某网页里面的部分内容.今天就拿我的Csdn的博客列表来举例. 打开页面  用Fir ...

  2. 开源项目Html Agility Pack实现快速解析Html

    这是个很好的的东西,以前做Html解析都是在用htmlparser,用的虽然顺手,但解析速度较慢,碰巧今天找到了这个,就拿过来试,一切出乎意料,非常爽,推荐给各位使用. 下面是一些简单的使用技巧,希望 ...

  3. Html Agility Pack基础类介绍及运用

    第一篇只对Html Agility Pack做了一个大概的介绍,在接下来的章节会比较深入的介绍Html Agility Pack. Html Agility Pack 源码中的类大概有28个左右,其实 ...

  4. HTML WEB 和HTML Agility Pack结合

    现在,在不少应用场合中都希望做到数据抓取,特别是基于网页部分的抓取.其实网页抓取的过程实际上是通过编程的方法,去抓取不同网站网页后,再进行分析筛选的过程.比如,有的比较购物网站,会同时去抓取不同购物网 ...

  5. 一款很不错的html转xml工具-Html Agility Pack

    之前发个一篇关于实现html转成xml的劣作<实现html转Xml>,受到不少网友的关心.该实现方法是借助htmlparser去分解html内容,然后按照dom的结构逐个生成xml字符串. ...

  6. Html Agility Pack解析HTML页

    文章来源:Html Agility Pack解析HTML页 现在,在不少应用场合中都希望做到数据抓取,特别是基于网页部分的抓取.其实网页抓取的过程实际上是通过编程的方法,去抓取不同网站网页后,再进行分 ...

  7. C#解析HTML利器-Html Agility Pack

    今天刚开始做毕设....好吧,的确有点晚.我的毕设设计需要爬取豆瓣的电影推荐,于是就需要解析爬取下来的html,之前用Python玩过解析,但目前我使用的是C#,我觉得C#不比python差,有微软大 ...

  8. 强大而灵活的的Html解析器——Html Agility Pack

    一.概述 Html Agility Pack 简称HAP,是一个强大而灵活的解析Html DOM的.Net类库. 二.官方链接 官网:http://html-agility-pack.net/ NuG ...

  9. C# 网络爬虫利器之Html Agility Pack如何快速实现解析Html

    简介 现在越来越多的场景需要我们使用网络爬虫,抓取相关数据便于我们使用,今天我们要讲的主角Html Agility Pack是在爬取的过程当中,能够高效的解析我们抓取到的html数据. 优势 在.NE ...

  10. 使用Html Agility Pack快速解析Html内容

    Html Agility Pack 是一个开源的.NET 方案HTML解析器. 开源地址:https://github.com/zzzprojects/html-agility-pack 用法:vs上 ...

随机推荐

  1. 关于JAVA SESSION的小测试

    手生就要多练啊... package com.jeelearning.servlet; import java.io.IOException; import java.io.PrintWriter; ...

  2. delphi 开机自动运行代码

    unit Unit1;//download by http://www.codefans.netinterface uses  Windows,Registry, Messages, SysUtils ...

  3. AC日记——Valued Keys codeforces 801B

    801B - Valued Keys 思路: 水题... 来,上代码: #include <cstdio> #include <cstring> #include <io ...

  4. JS基础用法-向数组指定位置插入对象

    在做省市区三级联动的时候,需要在省市区默认位置放上请选择字样. 由于后台的API接口返回的没有请选择字样,那么就需要给返回的数组手动增加请选择 代码如下 // 原来的数组 var array = [& ...

  5. POJ 2923 【01背包+状态压缩/状压DP】

    题目链接 Emma and Eric are moving to their new house they bought after returning from their honeymoon. F ...

  6. springMVC笔记:@ResponseBody

    使用@ResponseBody的方式,将Map形返回值转为json,用作POST请求的返回值.为了解决406 Not Acceptable错误,需要检查以下几项: 1. 依赖包中包含jackson-m ...

  7. Path Sum II (Find Path in Tree) -- LeetCode

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

  8. Multithreading and Grand Central Dispatch on iOS for Beginners Tutorial

    Have you ever written an app where you tried to do something, and there was a long pause while the U ...

  9. 【转】Ehcache详细解读

    Ehcache 是现在最流行的纯Java开源缓存框架,配置简单.结构清晰.功能强大,最初知道它,是从Hibernate的缓存开始的.网上中文的EhCache材料 以简单介绍和配置方法居多,如果你有这方 ...

  10. Navicat 破解版的安装

    因为电脑系统换掉,重装系统,重新配置了一下环境,安装Navicat,现记录一下过程,以便下次查询使用. 我们首先百度搜索一款navicat for mysql然后进行下载. 2 当我们下载完成之后首先 ...