VB.net使用HtmlAgilityPack从谷歌搜索中提取链接

google-search html-agility-pack vb.net

我现在更新了我的代码作为测试我要列出所有带有index.php一词的网址,但它也会显示其他内容。

这是我的工作代码:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim webClient As New System.Net.WebClient
    Dim WebSource As String = webClient.DownloadString("http://www.google.com/search?lr=&cr=countryCA&newwindow=1&hl=fil&as_qdr=all&biw=1366&bih=667&tbs=ctr%3AcountryCA&q=index.php&oq=index.php&gs_l=serp.12..0l10.520034.522335.0.525032.9.9.0.0.0.0.497.3073.1j1j2j0j5.9.0....0...1c.1.25.serp..5.4.884.J4smY262XgY")
    RichTextBox1.Text = WebSource

    ListBox1.Items.Clear()


    Dim htmlDoc As New HtmlAgilityPack.HtmlDocument()
    htmlDoc.LoadHtml(WebSource)

    For Each link As HtmlNode In htmlDoc.DocumentNode.SelectNodes("//cite")

        If link.InnerText.Contains("index.php") Then
            ListBox1.Items.Add(link.InnerText)
        End If

    Next

End Sub

预期输出应该只是在其上有index.php的网站,如下所示:

http://www.site1.com/index.php
http://www.site2.com/index.php
http://www.site3.com/index.php
http://www.site4.com/index.php
http://www.site5.com/index.php

但问题是它只会停止,直到index.php链接的其他部分不包括在内。

例如,完整的网址是

http://www.site5.com/index.php?test_test=test&test

程序只显示

http://www.site5.com/index.php

或者它会破碎点像

http://www.site5.com/index.php...test....test

一般承认的答案

我会使用Html Agility Pack来提取链接,如下所示

Dim links As New List(Of String)()
Dim htmlDoc As New HtmlAgilityPack.HtmlDocument()
htmlDoc.LoadHtml(WebSource)
For Each link As HtmlNode In htmlDoc.DocumentNode.SelectNodes("//a[@href]")
    Dim att As HtmlAttribute = link.Attributes("href")
    If att.Value.Contains("/forums/") Then
        links.Add(att.Value)
    End If
Next

如果是谷歌搜索结果尝试如下

For Each link As HtmlNode In htmlDoc.DocumentNode.SelectNodes("//cite")
    If link.InnerText.Contains("index.php") Then
        links.Add(link.InnerText)
    End If
Next


许可下: CC-BY-SA with attribution
不隶属于 Stack Overflow
这个KB合法吗? 是的,了解原因
许可下: CC-BY-SA with attribution
不隶属于 Stack Overflow
这个KB合法吗? 是的,了解原因