2013-12-19 79 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

相关问题