【问题标题】:Get innerText in Html Agility Pack (table)获取 Html Agility Pack 中的 innerText(表格)
【发布时间】:2019-09-17 05:55:12
【问题描述】:

我有一张这样的桌子

<table>
  <tr>
    <th>Some Text</th>
  </tr>
  <tr><td>
     ....
  </td>
</tr>
<tr>
  <th>
    <a href="" title="Students">Names</a>
  </th>
  <td>
      <a href="">Target Text 1</a>
      <a href="">Target Text 2</a>
  </td>
</tr>
<tr>
  <th>
    <a href="" title="Classes">ClassNo</a>
  </th>
  <td>
      <a href="">Some Text</a>
      <a href="">Some Text</a>
  </td>  
</tr>
.....

我需要获取目标文本。

“名称”是一个标题。它不是一个变量。 在这种情况下,我必须先到达带有 innerText = Names 的节点。

当我尝试这样的事情时;

htmlDoc.LoadHtml(html);

foreach (HtmlNode table in htmlDoc.DocumentNode.SelectNodes("//table"))
            {                
                foreach (HtmlNode row in table.SelectNodes("tr"))
                {                    
                    foreach (HtmlNode cell in row.SelectNodes("th|td"))
                    {                        
                        if (cell.InnerText.Contains("Names"))
                        {                    
                          MessageBox.Show(cell.NextSibling.InnerText);                                                       
                        }
                    }
                }
            }

输出就像单个文本。 (目标文本之间没有空格)因为我找不到访问各个链接的方法。我只能将它们作为单个文本获取。

Target Text 1Target Text 2

我也相信有更好、更合适的方式来获取这些文本。但我找不到。如果可以搜索innerText "Names"(它是&lt;th&gt;)然后在&lt;td&gt; 中得到&lt;a&gt;,那就太好了。

链接可以超过2个。所以基本上我需要得到所有链接。

<td>
      <a href="">Target Text 1</a>
      <a href="">Target Text 2</a>
      <a href="">Target Text 3</a>
      <a href="">Target Text 4</a>
  </td>

【问题讨论】:

    标签: c# .net html-agility-pack


    【解决方案1】:
    // @nuget: HtmlAgilityPack
    
    using System;
    using System.Xml;
    using System.Linq;
    using HtmlAgilityPack;
    public class Program
    {
        public static void Main(string[] args)
            {
                var html =
           @"<table>
            <tr>
                <th>
                    <a href='' title='other'>other row</a>
                </th>
                <td>
                  <a href=''>Target Text 1</a>
                  <a href=''>Target Text 2</a>
                </td>
            </tr>
            <tr>
                <th>
                    <a href='' title='Students'>Names</a>
                </th>
                <td>
                  <a href=''>Target Text 1</a>
                  <a href=''>Target Text 2</a>
                </td>
            </tr>
            <tr>
                  <th>
                    <a href='' title='Classes'>ClassNo</a>
                  </th>
                  <td>
                      <a href=''>Some Text</a>
                      <a href=''>Some Text</a>
                  </td>  
            </tr>";
    
                var htmlDoc = new HtmlDocument();
                htmlDoc.LoadHtml(html);
                var node = htmlDoc.DocumentNode.Descendants("a").Where(x => x.Attributes["title"] != null && x.Attributes["title"].Value == "Students").First();
    
                Console.WriteLine(node.ParentNode.SelectNodes("//td").First().OuterHtml);
    
            }
    }
    

    【讨论】:

    • 感谢您的回答。如果“名称”行是表中的第一行,它会起作用。但是“名称”行之前还有其他一些行。所以“Students -> Names”行不是第一行。
    • 对不起,你是对的。编辑了答案,更改了节点行: var node = htmlDoc.DocumentNode.Descendants("a").Where(x => x.Attributes["title"] != null && x.Attributes["title"].Value == "学生").First();
    • 在“names”测试之前添加了其他行,它似乎有效
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-18
    • 2012-03-07
    • 1970-01-01
    • 1970-01-01
    • 2018-10-30
    • 2012-02-24
    • 1970-01-01
    相关资源
    最近更新 更多