HtmlAgilityPack是.NET下的一个开源的HTML解析类库。支持用XPath来解析HTML。命名空间:HtmlAgilityPack。

1、读取网络中html网页内容,获取网页中元素body内的html,处理所有img元素的src属性后以字符串返回

                    if (l_sWenBenHtmlFtpPath.Substring(l_sWenBenHtmlFtpPath.LastIndexOf(".") + ) == "html")  
{
HtmlWeb htmlWeb = new HtmlWeb();
HtmlDocument htmlDoc = htmlWeb.Load(l_sWenBenHtmlFtpPath);
HtmlNode htmlNode = htmlDoc.DocumentNode;
HtmlNodeCollection nodes = htmlNode.SelectNodes("//body"); //使用xpath语法进行查询
if (nodes != null)
{
foreach (HtmlNode bodyTag in nodes)
{
HtmlNodeCollection nodes2 = htmlNode.SelectNodes("//img"); //使用xpath语法进行查询
if (nodes2 != null)
{
foreach (HtmlNode imgTag in nodes2)
{
string imgHttpPath = imgTag.Attributes["src"].Value;
imgTag.Attributes["src"].Value = l_sWenBenHtmlFtpPath.Substring(, l_sWenBenHtmlFtpPath.LastIndexOf("/") + ) + imgHttpPath;
}
}
l_sWenBenHtml = bodyTag.InnerHtml;
}
}
}

2、通过HtmlAgilityPack Html操作类库将html格式的字符串加载为html文档对象,再对html dom进行操作

                //1.解码前台提交的html字串
string sDecodeString = HttpUtility.HtmlDecode(HttpUtility.UrlDecode(sEncodeString));
//2.拼接成完整的html字串
sDecodeString = @"<!DOCTYPE html><html><head><meta http-equiv=""content-type"" content=""text/html;charset=UTF-8""/>"
+ @"</head><body><div>"
                  + sDecodeString + @"</div></body></html>";
//3.处理html的img标签的src属性-C#的HTML DOM操作
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(sDecodeString.Replace("\n", " "));
HtmlNode node = doc.DocumentNode;
HtmlNodeCollection nodes = node.SelectNodes("//img"); //使用xpath语法进行查询
if (nodes != null) //没有img节点时出错
{
//处理html字符串中img标签的src属性
foreach (HtmlNode imgTag in nodes)
{
string imgHttpPath = imgTag.Attributes["src"].Value;
imgHttpPath = imgHttpPath.Substring(imgHttpPath.LastIndexOf("/") + );
imgTag.Attributes["src"].Value = imgHttpPath;
}
}
//4.获取处理后的html字符串
sHtmlString = node.OuterHtml; //处理img中src属性后的html字符串
                //5.将字符串存入html格式的文件中
                //do something           

......

HtmlAgilityPack HTML操作类库的使用的更多相关文章

  1. mysql操作类库--摘抄

    <!--?php /** +---------------------------------- * MySQL操作类库 +---------------------------------- ...

  2. FtpHelper ftp操作类库

    FtpHelper ftp操作类库 using System; using System.Collections.Generic; using System.Linq; using System.Te ...

  3. 仅100行的JavaScript DOM操作类库

    如果你构建过Web引用程序,你可能处理过很多DOM操作.访问和操作DOM元素几乎是每一个Web应用程序的通用需求.我们我们经常从不同的控件收集信息,我们需要设置value值,修改div或span标签的 ...

  4. atitit.浏览器web gui操作类库 和 操作chrome浏览器的类库使用总结

    atitit.浏览器web gui操作类库 和 操作chrome浏览器的类库使用总结 1. 常见标准 1 1.1. 录制重放 1 1.2. 一个窗体一个proxy cookie 1 1.3. exec ...

  5. 史林枫:开源HtmlAgilityPack公共小类库封装 - 网页采集(爬虫)辅助解析利器【附源码+可视化工具推荐】

    做开发的,可能都做过信息采集相关的程序,史林枫也经常做一些数据采集或某些网站的业务办理自动化操作软件. 获取目标网页的信息很简单,使用网络编程,利用HttpWebResponse.HttpWebReq ...

  6. Excel操作类库最常用到的4种开源项目与MS Excel类库写操作对比分析性能

    4种开源Excel读写类库与MS Excel类库写操作对比 软件开发过程中,经常需要将数据保存为.xls或.xlsx文件.之前发现微软提供的Microsoft.Office.Interop.Excel ...

  7. C#常用操作类库三(XML操作类)

    /// <summary> /// XmlHelper 的摘要说明. /// xml操作类 /// </summary> public class XmlHelper { pr ...

  8. .net链接Oracle数据操作类库

    public abstract class OracleHelper { public OracleHelper() { } /// <summary> /// 数据库连接字符串 /// ...

  9. Gs_Class.Gs_DataFunction数据操作类库20160225

    using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security ...

随机推荐

  1. main函数的参数的用法

    说明:main函数的参数的用法源代码: #include <stdio.h>#include <stdlib.h>int main(int argc, char *argv[] ...

  2. java-08多态与异常处理

    1.运行以下代码: public class ParentChildTest { public static void main(String[] args) { Parent parent=new ...

  3. 【HTML5】Canvas绘制基础

    什么是 Canvas? HTML5 的 canvas 元素使用 JavaScript 在网页上绘制图像. 画布是一个矩形区域,您可以控制其每一像素. canvas 拥有多种绘制路径.矩形.圆形.字符以 ...

  4. bzoj 1951 [Sdoi2010]古代猪文 ——数学综合

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1951 数学综合题. 费马小定理得指数可以%999911658,又发现这个数可以质因数分解.所 ...

  5. Spring Boot基本配置

    本文参考javaEE开发的颠覆者SpringBoot实战第一版 基本配置 入口类和@SpringBootApplication Spring Boot通常有一个名为*Application的入口类,且 ...

  6. unix下网络编程之I/O复用(一)

    什么是I/O复用? What we need is the capability to tell the kernel that we want to be notified if one or mo ...

  7. Azure上Linux VM误配防火墙的恢复方法

    在实际运维中,防火墙把自己挡在机器外面的情况会时有发生.如何快速的恢复对运维人员是很重要的. 本文将介绍如何用Azure Extension实现不通过ssh对VM进行操作的方法. 之前写过一遍Blog ...

  8. Mybatis-Spring包学习

    MyBatis-Spring包用来将MyBatis无缝整合到Spring中.使用这个类库中的类, Spring将会加载必要的MyBatis工厂类和Session类. 这个类库也提供一个简单的方式来注入 ...

  9. js防止重复点击

    表单元素 disabled 没有之一. el.prop('disabled', true); ajax({}).done(function() { el.prop('disabled', false) ...

  10. 谈谈开发文本转URL小工具的思路

    URL提供了一种定位互联网上任意资源的手段,由于采用HTTP协议的URL能在互联网上自由传播和使用,所以能大行其道.在软件开发.测试甚至部署的环节,URL几乎可以说无处不再,其中用来定位文本的URL数 ...