【问题标题】:Parsing data from HTML using HTMLAgilityPack使用 HTMLAgilityPack 从 HTML 解析数据
【发布时间】:2013-10-31 16:30:11
【问题描述】:

从下面的源代码中,我想提取 InnerText“我的名字”,但我可以挑出 1 个 href 节点,而不是获取整个 href 列表:

<tr class="index">
  <td class="number">1.</td>
  <td class="image">
    <a href="/image/520211/" title="My index">
    <img src=" /images/M/MV5MDE.jpg" height="74" width="54" alt="My Alt" title="My Title">
    </a>
  </td>
  <td class="name">
    <span class="name_wrapper" data-size="small" data-caller-name="search">
    </span>
    <a href="/data/520211/">My Name</a>
    <span class="year">1974</span>
  </td>
</tr>

到目前为止我的代码:

for (var index = 0; index < htmlDocument.DocumentNode.SelectNodes("//tr[@class=index']//a[@href]").Count; index++)
{
    var item = htmlDocument.DocumentNode.SelectNodes("//tr[@class=index']//a[@href]")[index];
    MessageBox.Show(item.InnerText);
}

【问题讨论】:

    标签: c# html parsing html-agility-pack


    【解决方案1】:

    试试这个:

    string name = "";
    var node = htmlDocument.DocumentNode
        .SelectSingleNode("//tr[@class='index']//td[@class='name']//a[@href]");
    if (node != null)
        name = node.InnerText;
    

    【讨论】:

      猜你喜欢
      • 2017-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-26
      • 2016-11-20
      • 2010-12-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多