2

I have a written a program which downloads a webpage's source but now I want to search the source for a particular link I know the link is written like this:

<a href="/internet/A2/"><b>Geographical Survey Work</b></a>

Is there anyway of using "Geographical Survey Work" as criteria to retrieve the link? The code I am using to download the source to a string is this:

Dim sourcecode As String = ((New Net.WebClient).DownloadString("http://examplesite.com"))

So just to clarify I want to type into an input box "Geographical Survey Work" for instance and "/internet/A2" to popup in a messagebox? I think it can be done using a regex, but that's a bit beyond me. Any help would be great.

4

1 回答 1

0

使用 HTMLAgilityPack:

Dim vsPageHTML As String = "<html>... your webpage HTML code ...</html>"
Dim voHTMLDoc.LoadHtml(vsPageHTML) : vsPageHTML = ""
Dim vsURI As String = ""
Dim voNodes As HtmlAgilityPack.HtmlNodeCollection = voHTMLDoc.SelectNodes("//a[@href]")
If Not IsNothing(voNodes) Then
    For Each voNode As HtmlAgilityPack.HtmlNode In voNodes
        If voNode.innerHTML.toLower() = "<b>geographical survey work</b>" Then
            vsURI = voNode.GetAttributeValue("href", "")
            Exit For
        End If
    Next
End If
voNodes = Nothing : voHTMLDoc = Nothing

用 vsURI 做任何你想做的事。当我徒手编写时,您可能需要稍微调整一下代码。

于 2012-12-24T17:42:21.363 回答