在VB.Net中单击链接的最有效方法是什么?

时间:2013-12-19 20:52:28

标签: vb.net visual-studio-2012

我想点击海盗湾网址上的第一个链接(不是为了恶意目的,它只是个人项目)而且我想知道这是否是最好的方法:

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)

然而,消息框中没有任何内容,它只是空白,我传递的网址是错误的吗?

* 编辑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永远不会被正确设置。它只会产生一个空白字符串

1 个答案:

答案 0 :(得分:0)

使用WebClient将HTML作为字符串获取.DownloadString将比使用浏览器更有效,然后解析字符串以获得您所追求的内容。

我不完全确定你会如何做到这一点,但从理论上讲,你可以在下载时解析数据,然后在找到你需要的东西后取消操作 - 虽然这可能有点过头了。

    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