如何从c#中获取google html搜索结果的链接?

时间:2015-03-16 05:13:07

标签: c# html-parser

我收到了这段代码,它将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 个答案:

答案 0 :(得分:0)

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;
        }