2012-12-19 132 views
2

我有一個寫了一個程序,下載網頁的源代碼,但現在我想搜索的來源,因爲我知道這個鏈接是這樣寫的具體路段:如何在Visualbasic.net中搜索特定超鏈接的字符串?

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

反正是有使用「地理調查工作「作爲檢索鏈接的標準?我使用的下載源爲一個字符串的代碼是這樣的:

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

所以只是爲了澄清我要輸入到輸入框「地理調查工作」的實例和「/網絡/ A2」,以彈出在一個消息框?我認爲這可以使用正則表達式來完成,但這有點超出我的想法。任何幫助都會很棒。

+1

您可以使用HtmlAgilityPack API。 – adatapost

+2

不要使用正則表達式來解析html – K3N

+0

相關:[XPath:通過鏈接文本查找鏈接URL](http://stackoverflow.com/questions/915338/xpath-find-link-url-by-link-text) – sloth

回答

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。 您可能需要稍微調整一下代碼,因爲我正在寫手。

相關問題