HtmlAgilityPack scraping "href"

c# html html-agility-pack

Question

I wrote this code.:

Warning, the link point to adult site!!!

        var getHtmlWeb = new HtmlWeb();
        var document = getHtmlWeb.Load("http://xhamster.com/movies/2808613/jewel_is_a_sexy_cougar_who_loves_to_fuck_lucky_younger_guys.html");
        var aTags = document.DocumentNode.SelectNodes("//div[contains(@class,'noFlash')]"); 


        if (aTags != null)

        foreach (var aTag in aTags)
        {

            var href = aTag.Attributes["href"].Value;

            textBox2.Text = href;

        } 

I got an error when i try run this programm.

If i put other things in "var href" for example.:

var href = aTag.InnerHtml

I got the inner text, and i can see there the "href=" link, and some other datas.

But i need only the link after the href!

Accepted Answer

You are selecting div elements. A div element can't have href attribute.If you want to get href's of anchor tags you can use:

var hrefs = aTags.Descendants("a")
           .Select(node => node.GetAttributeValue("href","")) 
           .ToList();



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why