whoah
whoah

Reputation: 4443

searching google with HAP

How can I get href attribute from <a class='l'> ?

This is my code - it use HtmlAgilityPack to load the HTML source, but when iterating over SelectNodes("//a[@class='l']") in the foreach loop doesn't work.

Any ideas?

HtmlWeb siec = new HtmlWeb();
HtmlAgilityPack.HtmlDocument htmldokument = siec.Load(@"https://www.google.pl/search?q=beer");
List<string> list = new List<string>();

if (htmldokument != null)
{
    foreach (HtmlNode text in htmldokument.DocumentNode.SelectNodes("//a[@class='l']"))
    {

        list.Add(text.InnerHtml);
        Console.WriteLine(text.InnerHtml);
    }
}
Console.ReadKey();

Upvotes: 1

Views: 147

Answers (1)

Oscar Mederos
Oscar Mederos

Reputation: 29843

You can access to HtmlNode attributes like that:

node.Attributes["name"].Value

or even better (just in case the attribute does not exist):

node.GetAttributeValue("name", "")

If the attribute name is not found, an empty string will be returned.


HtmlWeb siec = new HtmlWeb();
HtmlAgilityPack.HtmlDocument htmldokument = siec.Load(@"https://www.google.pl/search?q=beer");
List<string> list = new List<string>();

if (htmldokument != null)
{
    foreach (HtmlNode text in htmldokument.DocumentNode.SelectNodes("//a[@class='l']"))
    {
        var href = text.GetAttributeValue("href", "");
        list.Add(href);
        Console.WriteLine(href);
    }
}
Console.ReadKey();

Upvotes: 1

Related Questions