HtmlAgilityPack HTML操作类库的使用

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

                    if (l_sWenBenHtmlFtpPath.Substring(l_sWenBenHtmlFtpPath.LastIndexOf(".") + 1) == "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(0, l_sWenBenHtmlFtpPath.LastIndexOf("/") + 1) + 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("/") + 1);
                                        imgTag.Attributes["src"].Value = imgHttpPath;
                                    }
                                }
                                //4.获取处理后的html字符串
                                sHtmlString = node.OuterHtml;    //处理img中src属性后的html字符串                //5.将字符串存入html格式的文件中                //do something

持续更新中,敬请期待...

时间: 2024-07-29 06:45:23

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

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

atitit.浏览器web gui操作类库 和 操作chrome浏览器的类库使用总结 1. 常见标准 1 1.1. 录制重放 1 1.2. 一个窗体一个proxy cookie 1 1.3. exec js 1 1.4. js 调用java 1 1.5. 修改dom属性 2 1.6. 关键字驱动 2 1.7. 加载js类库 2 1.8. 一个窗口一个代理 2 1.9. 独立窗口cookie 2 1.10. 无图模式支持 2 1.11. 支持自定义路径 2 2. 框架选型selenium2(web

mysql操作类库--摘抄

<!--?php /** +---------------------------------- * MySQL操作类库 +---------------------------------- * @author 马犇 <www.imaben.com--> +---------------------------------- * @version 1.2 (2013-5-31) +---------------------------------- */ include (dirnam

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

/// <summary> /// XmlHelper 的摘要说明. /// xml操作类 /// </summary> public class XmlHelper { protected string strXmlFile; protected XmlDocument objXmlDoc = new XmlDocument(); public XmlHelper(string XmlFile) { // // TODO: 在这里加入建构函式的程序代码 // try { objX

Gs_Class.Gs_DataFunction数据操作类库20160225

using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;//using System.IO;using

C#常用操作类库一(验证类)

public class Validator    {        #region  验证输入字符串为数字        /// <summary>        /// 验证输入字符串为数字        /// </summary>        /// <param name="strln">输入字符</param>        /// <returns>返回一个bool类型的值</returns>   

C#常用操作类库五(电脑操作类)

/// <summary> /// Computer Information /// </summary> public class ComputerHelper { public string CpuID; public string MacAddress; public string DiskID; public string IpAddress; public string LoginUserName; public string ComputerName; public s

Newtonsoft Json操作类库的使用

1.解析简单Json字符串 if (l_sSenJson != "") { JObject obj2 = (JObject)JsonConvert.DeserializeObject(l_sSenJson); //执行反序列化 if (obj2["ResultStatus"].ToString() == "true") { JObject obj3 = (JObject)JsonConvert.DeserializeObject(l_sReval

常用操作类库(验证类)

public class Validator    {        #region  验证输入字符串为数字        /// <summary>        /// 验证输入字符串为数字        /// </summary>        /// <param name="strln">输入字符</param>        /// <returns>返回一个bool类型的值</returns>   

c# 配置文件App.config操作类库

public class ConfigOperator { #region 从配置文件获取Value /// <summary> /// 从配置文件获取Value /// </summary> /// <param name="key">配置文件中key字符串</param> /// <returns></returns> public static string GetValueFromConfig(string