Как получить ссылки из результатов поиска Google HTML в C#?
Я получил этот код, который приносит мне результаты поиска от Google в виде строки HTML:
WebClient webClient = new WebClient();
string htmlString = webClient.DownloadString("http://www.google.com/search?q=" + searchQuery);
Есть идеи, как извлечь из него только ссылки? Я думаю, что я ищу строку, но это не выглядит так элегантно...
Я нашел этот код
HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
htmlDoc.LoadHtml(htmlString);
var selectNodes = htmlDoc.DocumentNode.SelectNodes("//li[@class='g']");
foreach (var node in selectNodes)
{
//node.InnerText will give you the text content of the li tags ...
}
Но я получаю исключение, что var selectNodes = htmlDoc.DocumentNode.SelectNodes("//li[@class='g']");
нулевой...
1 ответ
HtmlDocument doc = new HtmlDocument();
doc.Load("file.htm");
HtmlNodeCollection links = doc.DocumentNode.SelectNodes("//*[@background or @lowsrc or @src or @href]");
foreach (HtmlNode link in links)
{
if (link.Attributes["background"] != null)
link.Attributes["background"].Value = _newPath + link.Attributes["background"].Value;
if (link.Attributes["href"] != null)
link.Attributes["href"].Value = _newPath + link.Attributes["href"].Value;(link.Attributes["href"] != null)
link.Attributes["lowsrc"].Value = _newPath + link.Attributes["href"].Value;
if (link.Attributes["src"] != null)
link.Attributes["src"].Value = _newPath + link.Attributes["src"].Value;
}