有人可以解释一下这段 HtmlAgilityPack 代码吗?

发布于 2024-10-08 19:06:41 字数 1165 浏览 0 评论 0原文

我已尽力通过代码添加注释,但我有点卡在某些部分。

// create a new instance of the HtmlDocument Class called doc
1: HtmlDocument doc = new HtmlDocument();

// the Load method is called here to load the variable result which is html 
// formatted into a string in a previous code snippet
2: doc.Load(new StringReader(result));

// a new variable called root with datatype HtmlNode is created here. 
// Im not sure what doc.DocumentNode refers to?
3: HtmlNode root = doc.DocumentNode;
4:  

// a list is getting constructed here. I haven't had much experience 
// with constructing lists yet
5: List<string> anchorTags = new List<string>();
6:  

// a foreach loop is used to loop through the html document to 
// extract html with 'a' attributes I think..      
7: foreach (HtmlNode link in root.SelectNodes("//a"))
8: {
// dont really know whats going on here
9:     string att = link.OuterHtml;
// dont really know whats going on here too
10:     anchorTags.Add(att)
11: }

我已从 这里。感谢 Farooq Kaiser

I've tried my best to add comments through the code but Im kind of stuck at certain parts.

// create a new instance of the HtmlDocument Class called doc
1: HtmlDocument doc = new HtmlDocument();

// the Load method is called here to load the variable result which is html 
// formatted into a string in a previous code snippet
2: doc.Load(new StringReader(result));

// a new variable called root with datatype HtmlNode is created here. 
// Im not sure what doc.DocumentNode refers to?
3: HtmlNode root = doc.DocumentNode;
4:  

// a list is getting constructed here. I haven't had much experience 
// with constructing lists yet
5: List<string> anchorTags = new List<string>();
6:  

// a foreach loop is used to loop through the html document to 
// extract html with 'a' attributes I think..      
7: foreach (HtmlNode link in root.SelectNodes("//a"))
8: {
// dont really know whats going on here
9:     string att = link.OuterHtml;
// dont really know whats going on here too
10:     anchorTags.Add(att)
11: }

I've lifted this code sample from here. Credit to Farooq Kaiser

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦断已成空 2024-10-15 19:06:41

在 HTML Agility Pack 术语中,“//a”表示“查找文档中任意位置名为“a”或“A”的所有标签”。有关 XPATH 的更一般帮助,请参阅 XPATH 文档(独立于 HTML 敏捷包)。因此,如果您的文档如下所示:

<div>
  <A href="xxx">anchor 1</a>
  <table ...>
    <a href="zzz">anchor 2</A>
  </table>
</div>

您将获得两个锚定 HTML 元素。 OuterHtml 表示节点的 HTML(包括节点本身),而 InnerHtml 仅表示节点的 HTML 内容。因此,这里的两个 OuterHtml 是:

  <A href="xxx">anchor 1</a>

<a href="zzz">anchor 2</A>

注意,我已指定“a”或“A”,因为 HAP 实现需要注意或 HTML 不区分大小写。并且“//A”默认不起作用。您需要使用小写字母指定标签。

In HTML Agility Pack terms, "//a" means "Find all tags named 'a' or 'A' anywhere in the document". See XPATH docs for a more general help on XPATH (independently from HTML agility pack). So if you document looks like this:

<div>
  <A href="xxx">anchor 1</a>
  <table ...>
    <a href="zzz">anchor 2</A>
  </table>
</div>

You will get the two anchor HTML elements. OuterHtml represents the node's HTML including the node itself, while InnerHtml represents only the node's HTML content. So, here the two OuterHtml are:

  <A href="xxx">anchor 1</a>

and

<a href="zzz">anchor 2</A>

Note I have specified 'a' or 'A' because HAP implementation takes care or HTML case insensitivity. And "//A" dos not work by default. You need to specify tags using lowercase.

┾廆蒐ゝ 2024-10-15 19:06:41

关键是 SelectNodes 方法。这部分使用 XPath 从 HTML 中获取与您的查询匹配的节点列表。

这是我学习 XPath 的地方: http://www.w3schools.com/xpath/default.asp

然后它只是遍历那些匹配的节点并获取 OuterHTML - 包括标签的完整 HTML,并将它们添加到字符串列表中。列表基本上只是一个数组,但更灵活。如果您只需要内容,而不需要封闭的标记,则可以使用 HtmlNode.InnerHTML 或 HtmlNode.InnerText。

The key is the SelectNodes method. This part used XPath to grab a list of nodes out of the HTML that matches your query.

This is where I learned my XPath: http://www.w3schools.com/xpath/default.asp

Then it just walks through those nodes that matched and gets the OuterHTML - the full HTML including the tags, and adds them to the list of strings. A List is basically just an array, but more flexible. If you only wanted the contents, and not the enclosing tags, you'd use HtmlNode.InnerHTML, or HtmlNode.InnerText.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文