HtmlAgilityPack 使用案例介绍

2023年2月19日 266点热度 0人点赞

安装Html Agility Pack

Html Agility Pack是一个开源的解析HTML元素的类库。可以在C#中直接操作,类似于XML操作方法。操作还是比较好理解的。
在NuGet中安装:

PM>Install-Package HtmlAgilityPack -Version 1.8.2

这里可以选择最新版本
https://www.nuget.org/packages/HtmlAgilityPack

官网:https://html-agility-pack.net/

HtmlAgilityPack 使用案例介绍

解析HTML

1、本地html文件解析

var path = @"test.html";    
var doc = new HtmlDocument();      
doc.Load(path);     
var node = doc.DocumentNode.SelectSingleNode("//body");

 

2、html字符串解析

var html = @"<!DOCTYPE html>  
<html>  
<body>  
    <h1>This is <b>bold</b> heading</h1>  
    <p>This is <u>underlined</u> paragraph</p>  
    <h2>This is <i>italic</i> heading</h2>  
</body>  
</html> ";  
  
var htmlDoc = new HtmlDocument();  
htmlDoc.LoadHtml(html);  
var htmlBody = htmlDoc.DocumentNode.SelectSingleNode("//body");  
Console.WriteLine(htmlBody.OuterHtml);

3、根据url地址解析HTML

var html = @"http://html-agility-pack.net/";
 HtmlWeb web = new HtmlWeb();
var htmlDoc = web.Load(html);
var node = htmlDoc.DocumentNode.SelectSingleNode("//head/title");
Console.WriteLine("Node Name: " + node.Name + "\n" + node.OuterHtml);

节点操作-获取、追加、删除

var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(html);
// 获取TD下input标签SelectNodes
var htmlNodes = htmlDoc.DocumentNode.SelectNodes("//td/input");
// 获取第一个匹配的节点SelectSingleNode
string name = htmlDoc.DocumentNode
    .SelectSingleNode("//td/input")
    .Attributes["value"].Value;
// 根据ID获取节点
var htmlSelect = htmlDoc.GetElementbyId("ID");
// 创建节点信息
HtmlNode h2Node = HtmlNode.CreateNode("<h2> This is h2 heading</h2>");
// 追加节点在末尾
htmlSelect.AppendChild(h2Node);
// 追加节点在起始位置
htmlSelect.PrependChild(h2Node);
 
// 下面是集合节点集合
var htmlBody = htmlDoc.DocumentNode.SelectSingleNode("//body");
        
HtmlNode h2Node = HtmlNode.CreateNode("<h2> This is h2 heading</h2>");
HtmlNode pNode1 = HtmlNode.CreateNode("<p> This is appended paragraph 1</p>");
HtmlNode pNode2 = HtmlNode.CreateNode("<p> This is appended paragraph 2</p>");
 
HtmlNodeCollection children = new HtmlNodeCollection(htmlBody);
 
children.Add(h2Node);
children.Add(pNode1);
children.Add(pNode2);
 
htmlBody.AppendChildren(children);
// 移除节点
var htmlBody = htmlDoc.DocumentNode.SelectSingleNode("//body");
HtmlNode node = htmlBody.ChildNodes[1];
node.Remove();  // 或者 node.RemoveAll();
// htmlBody.RemoveAllChildren();
 
// 最后上一个设置属性的例子
var h1Node = htmlDoc.DocumentNode.SelectSingleNode("//h1");
h1Node.Attributes.Append("style");
h1Node.SetAttributeValue("style", "color:blue");

实战--根据html字符串,获取图片地址

以下为html内容:

<div>
      情况说明:近期有老师反映参赛孩子作品及费用发给我们没有收到参赛结果等问题,经公司调查,是有一些不法分子将我们的海报及链接更改为他们自己的,以骗取费用。
</div>
 
<div>
      <br />
    &nbsp; &nbsp; &nbsp; &nbsp;<span style="color:#ff0000;">备注:任何人或单位以 的名义进行非法诈骗,此行为与 无关,请各位老师参赛时注意信息的准确性。</span></div>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  <br />
<div>
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2020年07月08日  <br />
    <br />
    <br />
    &nbsp; &nbsp; &nbsp; &nbsp;现附上诈骗情节比较严重的一个个体信息,请老师及家长们及时查看微信是否有此人。</div>
<div>
    &nbsp; &nbsp;&nbsp;<img alt="" src="/uploads/allimg/200708/10391-200FQ1233W31.png" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div>
<div>
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 篡改后的海报&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div>
<div style="text-align: center;">
    &nbsp;</div>
<div>
    <br />
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div>
<div>
    &nbsp;<img alt="" src="/uploads/allimg/200708/10391-200FQ12I1257.jpg" style="width: 420px; height: 592px;" /></div>
<div>
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;诈骗人微信</div>
<div>
    <br />
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div>
<div>
    &nbsp;</div>

代码:

// 变量 HtmlContent 的值是上面的 文本html
 
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(HtmlContent); //加载html
HtmlContent =System.Web.HttpUtility.UrlDecode(HtmlContent).Trim();//重新编码,并去除空格
HtmlNode HtmlContentNode = doc.DocumentNode;
string html = "";
if (HtmlContentNode != null)
{
    html = HtmlContentNode.InnerHtml;
// 代码中的.//img[@src]  是 XPath 路径代码,意思就是返回所有图片节点
    HtmlNodeCollection img = HtmlContentNode.SelectNodes(".//img[@src]"); //获取所有图片节点
    if (img != null)
    {
        foreach (var imgitem in img)
        {
            string href = imgitem.Attributes["src"].Value; //获取图片地址
 
             //你可以做一些动作,例如保存图片到本地
        }
    }
}
 
//替换图地址,给没有前缀的图片路径增加前缀
html.Replace("src=\"/uploads/",$"src=\"http://www.xxxx.com/uploads/")

 

胖二十

这个人很懒,什么都没留下

文章评论