如何从表中的标签获取href?

时间:2015-10-15 12:15:18

标签: c# web-services html-agility-pack

我有一个类似的网络方法,

public List<List<string>> HelloWorld() {

    WebClient webClient = new WebClient();
    string page = webClient.DownloadString("http://www.deu.edu.tr/DEUWeb/Guncel/v2_index_cron.html");

    HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
    doc.LoadHtml(page);

        List<List<string>> table = doc.DocumentNode.SelectSingleNode("//table")
        .Descendants("tr")
        .Skip(1)
        .Select(tr => tr.Elements("td").Select(td => td.InnerText.Trim()).ToList())
        .ToList();

    return table;
}

我已经获得了表格中的信息,但我想获得该表格的<a href="Link">链接信息。

我应该在方法中添加什么?我同时需要这两个信息。

HTML

<table>
   <tr>
     <td>
        <img/>
     </td>
     <td>
        <a href="what I want">Text</a>
     </td>
   </tr>
    .....
</table>

1 个答案:

答案 0 :(得分:0)

您可以使用jQuery,如下所示:

<a href="https://google.com" class="demo">Click me</a>
<script>
alert($('.demo').attr('href'));
</script>
相关问题