I'm trying to retrieve all links from a google search but without success ...
Im using Selenium + HTML Agility Pack
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(pageSource);
foreach (HtmlNode link in doc.DocumentNode.SelectNodes("//*[@id='rso']/div/div/div[1]/div/div/h3/a"))
{
string href = link.GetAttributeValue("data-href", string.Empty);
list.Add(href);
}
foreach (var item in list)
{
Console.WriteLine(item);
}
Google HTML
<a href="/url?sa=t&rct=j&q=&esrc=s&source=web&cd=2&cad=rja&uact=8&ved=0ahUKEwizrv_f06TWAhXLD5AKHa4pB1AQFgg3MAE&url=http%3A%2F%2Fwww.rtl.de%2Fcms%2Fdsds-2017-das-sind-die-votingergebnisse-der-14-staffel-4112962.html&usg=AFQjCNF_sFs_fpNAeBAPLitsVZbShMAhiw" onmousedown="return rwt(this,'','','','2','AFQjCNF_sFs_fpNAeBAPLitsVZbShMAhiw','','0ahUKEwizrv_f06TWAhXLD5AKHa4pB1AQFgg3MAE','','',event)" data-href="http://www.rtl.de/cms/dsds-2017-das-sind-die-votingergebnisse-der-14-staffel-4112962.html">DSDS 2017: Das sind die Votingergebnisse der 14. Staffel - RTL.de</a>
I would like to extract the date-href but without success
Isn't the problem that in this way you are finding all links.
When you add a[@data-href]
to the XPath
of SelectNodes
you'll select only the links with an actual data url attribute in them.
So it will become :
foreach (HtmlNode link in doc.DocumentNode.SelectNodes("//*[@id='rso']/div/div/div[1]/div/div/h3/a[@data-href]"))
{
string href = link.GetAttributeValue("data-href", string.Empty);
list.Add(href);
}