HtmlAgilityPack - 如何读取某些表 - c# 4.0

发布于 2024-12-10 17:04:24 字数 153 浏览 1 评论 0原文

使用 c# 4.0 和 htmlagilitypack 如何读取某个表中的值。我的意思是,假设有 10 个表,我想读取第 6 个表中的值,或者我有表 ID。

或者说我想读取某个 td 之后的 td 值。

或者在某些 div 或元素或文本之后的表格。这些可能吗?

With using c# 4.0 and htmlagilitypack how can i read values inside certain table. I mean lets say there are 10 tables and i want to read values from 6 th or i have table id.

Or lets say i want to read td value coming after certain td.

Or table coming after certain div or element or text. Are these possible ?

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

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

发布评论

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

评论(1

风筝有风,海豚有海 2024-12-17 17:04:24

您所询问的一切都可以相对容易地完成。缺少文档并不重要,它在使用和功能上都应该类似于 XML 和网络的 XmlDocument 实现。

如何读取某个表中的值?假设有 10 个表,我想读取第 6 个表中的值,或者我有表 ID。

查找第6个表:

// XPath
var table6 = doc.DocumentNode.SelectSingleNode("//table[6]");

// LINQ
var table6 = doc.DocumentNode.Descendants("table").Skip(5).FirstOrDefault();

通过id查找表/元素:

var myTable = doc.GetElementById("myTable");

// XPath
var myTable = doc.DocumentNode.SelectSingleNode("//table[@id='myTable']");
var myTable = doc.DocumentNode.SelectSingleNode("//*[@id='myTable']");

// LINQ
var myTable = doc.DocumentNode
    .Descendants("table")
    .Where(table => table.Attributes.Contains("id"))
    .SingleOrDefault(table => table.Attributes["id"].Value == "myTable");
var myTable = doc.DocumentNode
    .Descendants()
    .Where(e => e.Attributes.Contains("id"))
    .SingleOrDefault(e => e.Attributes["id"].Value == "myTable");
var myTable = doc.DocumentNode
    .Descendants("table")
    .SingleOrDefault(table => table.GetAttributeValue("id", null) == "myTable");
var myTable = doc.DocumentNode
    .Descendants()
    .SingleOrDefault(e => e.GetAttributeValue("id", null) == "myTable");

假设我想读取特定 td 之后的 td 值。

// XPath
var certainTd = table6.SelectSingleNode("//td[2]");
var tdAfterCertainTd = certainTd.SelectSingleNode("following-sibling::td[1]");

// LINQ (not so easy)
var certainTd = table6.Descendants("td").Skip(1).FirstOrDefault();
var tdAfterCertainTd = certainTd.NextSibling;
while (tdAfterCertainTd != null)
{
    if (tdAfterCertainTd.Name == "td")
        break;
    tdAfterCertainTd = tdAfterCertainTd.NextSibling;
}

表格出现在特定的 div 或元素或文本之后。

// XPath
var certainDiv = doc.DocumentNode.SelectSingleNode("//div[1]");
var tableAfterCertainDiv = certainDiv.SelectSingleNode("following-sibling::table[1]");

// LINQ (not so easy)
var certainDiv = doc.DocumentNode.Descendants("div").FirstOrDefault();
var tableAfterCertainDiv = certainDiv.NextSibling;
while (tableAfterCertainDiv != null)
{
    if (tableAfterCertainDiv.Name == "table")
        break;
    tableAfterCertainDiv = tableAfterCertainDiv.NextSibling;
}

您应该注意到一些模式。

Everything you have asked about could be done relatively easily. It doesn't matter that its documentation might be lacking, it should be similar to XML and the network's XmlDocument implementation in both use and functionality.

How can I read values inside certain table? Let's say there are 10 tables and I want to read values from 6th or I have table id.

Finding the 6th table:

// XPath
var table6 = doc.DocumentNode.SelectSingleNode("//table[6]");

// LINQ
var table6 = doc.DocumentNode.Descendants("table").Skip(5).FirstOrDefault();

Finding the table/element by id:

var myTable = doc.GetElementById("myTable");

// XPath
var myTable = doc.DocumentNode.SelectSingleNode("//table[@id='myTable']");
var myTable = doc.DocumentNode.SelectSingleNode("//*[@id='myTable']");

// LINQ
var myTable = doc.DocumentNode
    .Descendants("table")
    .Where(table => table.Attributes.Contains("id"))
    .SingleOrDefault(table => table.Attributes["id"].Value == "myTable");
var myTable = doc.DocumentNode
    .Descendants()
    .Where(e => e.Attributes.Contains("id"))
    .SingleOrDefault(e => e.Attributes["id"].Value == "myTable");
var myTable = doc.DocumentNode
    .Descendants("table")
    .SingleOrDefault(table => table.GetAttributeValue("id", null) == "myTable");
var myTable = doc.DocumentNode
    .Descendants()
    .SingleOrDefault(e => e.GetAttributeValue("id", null) == "myTable");

Let's say I want to read td value coming after certain td.

// XPath
var certainTd = table6.SelectSingleNode("//td[2]");
var tdAfterCertainTd = certainTd.SelectSingleNode("following-sibling::td[1]");

// LINQ (not so easy)
var certainTd = table6.Descendants("td").Skip(1).FirstOrDefault();
var tdAfterCertainTd = certainTd.NextSibling;
while (tdAfterCertainTd != null)
{
    if (tdAfterCertainTd.Name == "td")
        break;
    tdAfterCertainTd = tdAfterCertainTd.NextSibling;
}

Table coming after certain div or element or text.

// XPath
var certainDiv = doc.DocumentNode.SelectSingleNode("//div[1]");
var tableAfterCertainDiv = certainDiv.SelectSingleNode("following-sibling::table[1]");

// LINQ (not so easy)
var certainDiv = doc.DocumentNode.Descendants("div").FirstOrDefault();
var tableAfterCertainDiv = certainDiv.NextSibling;
while (tableAfterCertainDiv != null)
{
    if (tableAfterCertainDiv.Name == "table")
        break;
    tableAfterCertainDiv = tableAfterCertainDiv.NextSibling;
}

You should notice some patterns.

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