如何分别获得一个链接的标题和href的值与HTML敏捷性包? [英] How to get a link's title and href value separately with html agility pack?

查看:133
本文介绍了如何分别获得一个链接的标题和href的值与HTML敏捷性包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着去下载一个网页包含这样的表

Im trying to download a page contain a table like this

<table id="content-table">
  <tbody>
    <tr>
      <th id="name">Name</th>
      <th id="link">link</th>
    </tr>

    <tr class="tt_row">

      <td class="ttr_name">
       <a title="name_of_the_movie" href="#"><b>name_of_the_movie</b></a>
       <br>
       <span class="pre">message</span>
      </td>

      <td class="td_dl">
        <a href="download_link"><img alt="Download" src="#"></a>
      </td>

    </tr>

    <tr class="tt_row"> .... </tr>
    <tr class="tt_row"> .... </tr>
  </tbody>
</table>

我想提取TD类=ttr_name的name_of_the_movie以及TD类下载链接=td_dl

i want to extract the name_of_the_movie from td class="ttr_name" and download link from td class="td_dl"

这是通过表中的行用于循环codeI

this is the code i used to loop through table rows

HtmlAgilityPack.HtmlDocument hDocument = new HtmlAgilityPack.HtmlDocument();
hDocument.LoadHtml(htmlSource);
HtmlNode table = hDocument.DocumentNode.SelectSingleNode("//table");

foreach (var row in table.SelectNodes("//tr"))
{
  HtmlNode nameNode = row.SelectSingleNode("td[0]");
  HtmlNode linkNode = row.SelectSingleNode("td[1]");
}

目前我不知道如何检查里面的名称节点和linkNode和提取数据。

currently i have no idea how to check the nameNode and linkNode and extract data inside it

任何帮助,将AP preciated

any help would be appreciated

问候

推荐答案

我不能马上进行测试,但它应该是的线中的内容:

I can't test it right now, but it should be something among the lines of :

    string name= namenode.Element("a").Element("b").InnerText;
    string url= linknode.Element("a").GetAttributeValue("href","unknown");

这篇关于如何分别获得一个链接的标题和href的值与HTML敏捷性包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆