0

Hello im trying to get this "a" atrribute from this HTML source code using HtmlAgilityPack in C#.

            <table width='200'>
            <tr>
                <td width='50'>
                    <a href='index.php?action=shop&type=koszulka'>
                    <img src='images/lay_game/miasto/sklep.png' width='40' class="dymek" style='cursor:pointer;' title="Tutaj możesz kupić wyposażenie dla swojego zawodnika"  /></a>
                </td>
                <td>
                    <a href='index.php?action=shop&type=koszulka' >Sklepy</a>
                </td>
            </tr>
            <tr>
                <td width='50'>
                    <a href='index.php?action=37317|lbr5tlbphafc3cf30b08vl8601|trening|MCMxIzI=|a32a443dd66c39e8cce9a4903171d81b|162f3a6d72c860855a5dc3de18c8855c'>
                    <img src='images/lay_game/miasto/trening.png' width='40' class="dymek" style='cursor:pointer;' title="Chcesz podnieść swoje umiejętności? Dobrze trafiłeś"/></a>
                </td>
                <td>
                    <a href='index.php?action=37317|lbr5tlbphafc3cf30b08vl8601|trening|MCMxIzI=|a32a443dd66c39e8cce9a4903171d81b|162f3a6d72c860855a5dc3de18c8855c'>Trening</a>
                </td>
            </tr>
            <tr>
                <td width='50'>
                    <a href='index.php?action=hospital'>
                    <img src='images/lay_game/miasto/szpital.png' width='40' class="dymek" style='cursor:pointer;' title="Możesz tu zredukować zmęczenie, wyleczyć kontuzję lub podnieść formę"/></a>
                </td>
                <td>
                    <a href='index.php?action=hospital'>Szpital</a>
                </td>
            </tr>
            <tr>
                <td width='50'>
                    <a href='index.php?action=gielda'>
                    <img src='images/lay_game/miasto/centrum.png' width='40' class="dymek" style='cursor:pointer;' title="Chcesz zarobić i nie boisz się ryzyka? Zatem witamy na giełdzie FT"  /></a>
                </td>
                <td>
                    <a href='index.php?action=gielda'>Giełda</a>
                </td>
            </tr>
            <tr>
                <td width='50'>
                    <a href='index.php?action=pojedynek'>
                    <img src='images/lay_game/miasto/pojedynek.png' width='40' class="dymek" style='cursor:pointer;' title="Pojedynek Uliczny."  /></a>
                </td>
                <td>
                    <a href='index.php?action=pojedynek'>Pojedynek</a>
                </td>
            </tr>
        </table>

My target is a attribute with href="index.php?action=37317|lbr5tlbphafc3cf30b08vl8601|trening|MCMxIzI=|a32a443dd66c39e8cce9a4903171d81b|162f3a6d72c860855a5dc3de18c8855c"

I really dunno how to get this. My trying code is below:

HtmlAgilityPack.HtmlDocument HTMLParser = new HtmlAgilityPack.HtmlDocument();
HTMLParser.LoadHtml(result);                

string href;                
foreach (HtmlNode node in HTMLParser.DocumentNode.SelectNodes("//table//tr//td//a"))
{
     href = node.ChildNodes[0].InnerHtml;
} 

But it not working :(

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
Narkon
  • 398
  • 2
  • 17

2 Answers2

0

The following should work fine, assuming all you care about is that particular <a> tag:

HtmlNode anchor = HTMLParser.DocumentNode.SelectSingleNode(@"//table/tr[2]/td/a");
Andrew Whitaker
  • 124,656
  • 32
  • 289
  • 307
0

There are two <a> elements with href attribute you wanted. More importantly, it isn't clear how you want to identify that particular <a>. Assuming that you want to indentify by inner text "Trening", try this way :

HtmlNode a = HTMLParser.DocumentNode.SelectSingleNode(@"//table/tr/td/a[.='Trening']");
String href = a.GetAttributeValue("href", "");
har07
  • 88,338
  • 12
  • 84
  • 137