使用 HtmlAgilityPack 到 select innerHtml
using HtmlAgilityPack to select innerHtml
假设我已遵循 html 文档
<div class=" wrap_body text_align_left" style="">
<div class="some"> hello </div>
<div class="someother"> world </div>
hello world
</div>
我想提取这个
<div class="some"> hello </div>
<div class="someother"> world </div>
hello world
使用 C# 或 vb.net 使用 HtmlAgilityPack 进行提取的最佳方法是什么?
这是我的代码,直到完成但有些挣扎。
谢谢!
For Each no As HtmlAgilityPack.HtmlNode In docs.DocumentNode.SelectNodes("//div[contains(@class,'wrap_body')]")
Dim attr As String = no.GetAttributeValue("wrap_body", "")
Next
下面是获取内部的示例 Html
var html =
@"<body>
<div class='wrap_body text_align_left' style=''>
<div class='some'> hello </div>
<div class='someother'> world </div>
hello world
</div>
</body>";
var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(html);
var htmlNodes = htmlDoc.DocumentNode.SelectNodes("//body/div");
foreach (var node in htmlNodes)
{
Console.WriteLine(node.InnerHtml);
}
您可以使用 DocumentNode
方法的 SelectNodes
从 html 中检索特定节点。
class Program
{
static void Main(string[] args)
{
string htmlContent = File.ReadAllText(@"Your path to html file"); ;
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(htmlContent);
var innerContent = doc.DocumentNode.SelectNodes("/div").FirstOrDefault().InnerHtml;
Console.WriteLine(innerContent);
}
}
输出:
假设我已遵循 html 文档
<div class=" wrap_body text_align_left" style="">
<div class="some"> hello </div>
<div class="someother"> world </div>
hello world
</div>
我想提取这个
<div class="some"> hello </div>
<div class="someother"> world </div>
hello world
使用 C# 或 vb.net 使用 HtmlAgilityPack 进行提取的最佳方法是什么? 这是我的代码,直到完成但有些挣扎。 谢谢!
For Each no As HtmlAgilityPack.HtmlNode In docs.DocumentNode.SelectNodes("//div[contains(@class,'wrap_body')]")
Dim attr As String = no.GetAttributeValue("wrap_body", "")
Next
下面是获取内部的示例 Html
var html =
@"<body>
<div class='wrap_body text_align_left' style=''>
<div class='some'> hello </div>
<div class='someother'> world </div>
hello world
</div>
</body>";
var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(html);
var htmlNodes = htmlDoc.DocumentNode.SelectNodes("//body/div");
foreach (var node in htmlNodes)
{
Console.WriteLine(node.InnerHtml);
}
您可以使用 DocumentNode
方法的 SelectNodes
从 html 中检索特定节点。
class Program
{
static void Main(string[] args)
{
string htmlContent = File.ReadAllText(@"Your path to html file"); ;
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(htmlContent);
var innerContent = doc.DocumentNode.SelectNodes("/div").FirstOrDefault().InnerHtml;
Console.WriteLine(innerContent);
}
}
输出: