使用 HtmlAgilityPack 从 Bing 获取 href

Getting href from Bing using HtmlAgilityPack

使用此搜索 URL Bing:

http://www.bing.com/images/search?scope=images&sp=-1&pq=ferrari&sc=9-3&sk=&cvid=E471F1335E6A48C897DB5CEE745F51E1&q=ferrari&qft=+filterui:imagesize-large&FORM=R5IR3%22

我需要从 class 中获取 class "iusc" 搜索中 bing 返回的每张图片的 href 属性。

我使用的代码库是这样的:

For Each link As HtmlAgilityPack.HtmlNode In htmlDoc.DocumentNode.SelectNodes("//a[@href]")
debug.Print(link.GetAttributeValue("href", ""))
Next

但它 returns "murl" 属性,而不是 "href"。

那怎么获取href呢?

问题在于 xpath 表达式以及从 Web 加载文档的方式导致了一个空文档。试试这个:

Dim website As New HtmlWeb()
Dim doc As HtmlDocument = website.Load(url)
Dim links = doc.DocumentNode.SelectNodes("//a[contains(@class,'iusc')]")
For Each link In links
    Dim href As String = link.GetAttributeValue("href", "")
    Debug.Print(href)
Next

考虑到 Debug.Print() 会根据您配置 VS 的方式将输出发送到不同的 windows。如果您没有看到任何东西,请调试并检查 href 值。