如何在 c# 中从谷歌 html 搜索结果中获取链接



我得到了这段代码,它以HTML字符串的形式将Google的搜索结果带给我:

 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']");为空...

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

最新更新