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();
You can access to HtmlNode attributes like that:
node.Attributes["name"].Valueor even better (just in case the attribute does not exist):
If the attribute
nameis not found, an empty string will be returned.