开发者

Select part of InnerText using HtmlAgilityPack

开发者 https://www.devze.com 2023-03-05 11:40 出处:网络
How can I select a part of InnerText using HtmlAgilityPack, for example: <td class=\"playerName\" width=\"192\">

How can I select a part of InnerText using HtmlAgilityPack, for example:

<td class="playerName" width="192">
  <a href="/cricket/content/player/21585.html">player1</a>* 
</td>

Now I want to select 21585 开发者_如何学Gofrom the href attribute.


You can get to the HREF with XPATH & code, like this

    HtmlDocument doc = new HtmlDocument();
    doc.Load(myHtmlFilePath);
    // get to the A tag using XPATH
    HtmlNode a = doc.DocumentNode.SelectSingleNode("//td[@class='playerName']/a");
    // get the HREF attribute
    string href = a.GetAttributeValue("href", null);

but not beyond. You'll have to parse the href manually, here is a quick hack that works with your example:

    Uri uri = new Uri(@"dummy:" + href); // use whatever "drive-like" root
    Console.WriteLine(Path.GetFileNameWithoutExtension(uri.LocalPath));
0

精彩评论

暂无评论...
验证码 换一张
取 消