2013-12-19 93 views
1

我想點擊一個海盜灣的URL的第一個鏈接(不適用於邪惡的目的,它只是一個個人項目),我想知道這是否是做到這一點的最好辦法:在VB.Net中點擊鏈接的最有效方法是什麼?

For Each ele As HtmlElement In WebBrowser1.Document.Links 

    If ele.GetAttribute("href").Contains("magnet") Then 
     ele.InvokeMember("click") 
     Exit For 
    End If 

Next 

我我想知道這是否是點擊頁面上第一個磁鏈接的最佳方式,我目前正在使用網絡瀏覽器,但是我想知道是否可以不使用它?也許有一個HTTP請求或這些行的東西?

* 編輯GJKH *

我有這樣的代碼:

Dim PBsource As String = New System.Net.WebClient().DownloadString("http://pirateproxy.se/search/ubuntu/0/7/0") 
MsgBox(PBsource) 

但是沒有出現在消息框,它只是一片空白,我在經過URL錯了嗎?

* EDIT 2 *

我有這樣的代碼在我的按鈕子:

Imports System.Text.RegularExpressions 
Private Sub btnTest_Click(sender As Object, e As EventArgs) Handles btnTest.Click 

Dim PBsource As String = New System.Net.WebClient().DownloadString("http://pirateproxy.se/search/ubuntu/0/7/0") 
MsgBox(PBsource) 

Dim strReg As String 
'Regex to get a herf links 
strReg = "<a\s+href\s*=\s*""?([^"" >]+)""?>(.+)</a>" 
Dim reg As New Regex(strReg, RegexOptions.IgnoreCase) 
Dim m As Match = reg.Match(PBsource) 
Dim magnetURL As String = "" 
'Keep going while we hit regex matches 
While m.Success 
    If m.Groups(1).Value.ToString.Contains("magnet") Then 
     'Match found, assign magnetURL and exit while 
     magnetURL = m.Groups(1).ToString 
     Exit While 
    End If 
    'Match not found, move to next match 
    m = m.NextMatch() 
End While 


If Not magnetURL Is String.Empty Then 
    Using wc As New System.Net.WebClient 
     wc.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)") 
     PBsource = wc.DownloadString("magnet:?xt=urn:btih:1e4dae83371ba704d5d89e1828068ef0c4151e32&dn=Steam+OS+Official+Installer&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=udp%3A%2F%2Ftracker.publicbt.com%3A80&tr=udp%3A%2F%2Ftracker.istole.it%3A6969&tr=udp%3A%2F%2Ftracker.ccc.de%3A80&tr=udp%3A%2F%2Fopen.demonii.com%3A1337") 
     MsgBox(PBSource) 
    End Using 
Else 
    MsgBox("no magnet URL found") 
End If 
End Sub 

但是不管是什麼似乎PBSource永遠不會被設置正確。這隻會導致一個空字符串

+0

你可以得到的頁面,使用HTMLAgilityPack解析它,然後「點擊」鏈接 –

+0

你的代碼是所有的地方,看到我更新的答案,只是複製和粘貼。 – GJKH

+0

@GJKH謝謝,我在這裏沒有深入,最初是作爲一個項目開始學習循環。感謝你現在完美的幫助,謝謝。 – SCGB

回答

0

使用WebClient.DownloadString獲取HTML作爲字符串將比使用瀏覽器更有效,那麼這是一個解析字符串以獲取您的內容的情況。

我不完全確定你會如何去做這件事,但理論上你可以解析數據,因爲它正在下載,然後取消操作,一旦你找到你所需要的 - 這可能是過度殺傷。

Using wc As New System.Net.WebClient 
     wc.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)") 
     Dim PBSource = wc.DownloadString("http://pirateproxy.se/search/ubuntu/0/7/0") 

     Dim strReg As String 
     'Regex to get a herf links 
     strReg = "\<a.+?href=(?<q>["" '])(.+?)\k<q>.*?>([^\<]+)" 
     Dim reg As New Regex(strReg, RegexOptions.IgnoreCase) 

     Dim m As Match = reg.Match(PBSource) 

     Dim magnetURL As String = "" 


     'Keep going while we hit regex matches 
     While m.Success 
      If m.Groups(1).Value.ToString.Contains("magnet") Then 
       'Match found, assign magnetURL and exit while 
       magnetURL = m.Groups(1).ToString 
       Exit While 
      End If 
      'Match not found, move to next match 
      m = m.NextMatch() 
     End While 

     If Not magnetURL Is String.Empty Then 
      Dim a = MsgBox("Would you like to open:" & vbCrLf & vbCrLf & magnetURL, MsgBoxStyle.YesNo) 
      If a = MsgBoxResult.Yes Then Process.Start(magnetURL) 
     Else 
      MsgBox("no magnet URLS found") 
     End If 

    End Using 
+0

剛剛測試過這個與非磁鐵網址,併爲我工作,您可能需要刪除前導和尾隨''' – GJKH

+0

感謝您的答覆,從未使用正則表達式,但我願意嘗試。你是什​​麼意思,我可能需要刪除一個領先的和尾隨的'? – SCGB

+0

在我的測試中,一些URL有撇號,所以刪除它們 - Process.Start(Replace(magnetURL,''「,」「)) – GJKH

相關問題