我可以从带有下载链接的网站下载文件吗? Visual Basic

时间:2014-12-25 23:09:50

标签: html vb.net download

是否可以从提供下载按钮的网站下载文件?我正在尝试制作一个简单的程序,它将为Minecraft下载纹理包并自动安装它们。当提示输入纹理包的URL时,用户必须使用直接下载URL,这意味着右键单击浏览器中的下载按钮,然后单击“复制链接位置”。这似乎比下载和手动安装更复杂,所以是否可以使用下载页面上提供的链接?

我不知道怎么做,但我有一个效率很低的方法 - 输入URL,下载HTML页面。然后扫描文件以查找看起来像下载链接的字符串,并进行测试。如果它返回一些东西,那么快乐的日子。如果没有,它将在HTML文件中查找另一个下载链接。

我想不出任何有用的东西,甚至看起来有点可疑。对不起,如果我听起来很天真,我是编程新手。

1 个答案:

答案 0 :(得分:0)

好。所以这是一个可能的解决方案。

可悲的是,它仅在链接完成时才有效。 (以http:// etc开头......)

Imports System.Text.RegularExpressions
Imports System.IO

Public Class Form1
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        ScanForZIPs()
    End Sub

    Private Sub ScanForZIPs()
        Dim Reader As New StreamReader("<path to the downloaded webpage>")
        Dim HTMLSource As String = Reader.ReadToEnd()

        Dim Pattern As String = "(?<Protocol>\w+):\/\/(?<Domain>[\w@][\w.:@]+)\/?[\w\.?=%&=\-@/$,]*" 'Pattern, which the Regex will use in order to match links in the webpage.
        'Credits to IronRazerz (https://social.msdn.microsoft.com/profile/ironrazerz/?ws=usercard-mini) for giving me a fully working pattern.
        Dim RgEx As New Regex(Pattern, RegexOptions.IgnoreCase) 'Define the Regex.

        Dim mc As MatchCollection = RgEx.Matches(HTMLSource) 'Check for matches in the HTML source.

        Dim MatchList As New List(Of String) 'List of strings for the matched links.

        For Each m As Match In mc 'Loop through each match.
            MatchList.Add(m.Value) 'Add the value (link) of each match to the MatchList.
        Next

        Dim ZipsList As New List(Of String) 'List of links that ends with .zip.

        For Each s As String In MatchList 'Loop through each string in MatchList.
            If s.ToLower.ToString.EndsWith(".zip") = True Then 'Check if the link ends with .zip.
                ZipsList.Add(s) 'Add the link to the list.
            End If
        Next

        MessageBox.Show(ZipsList.Count & " .zip files found", "", MessageBoxButtons.OK, MessageBoxIcon.Information) 'Display how many .zip files were found on the page.

        Dim SelectZip As New SelectDownload 'Define a new download form.
        For Each z As String In ZipsList 'Loop through the found .zip links.
            SelectZip.ListBox1.Items.Add(z) 'Add them to the list in the SelectZip form.
        Next
        SelectZip.ListBox1.HorizontalScrollbar = True 'Horizontall scrollbar in SelectZip's ListBox.
        SelectZip.ShowDialog() 'Display the SelectZip form.
    End Sub
End Class

SelectDownload表单。 :)

The SelectDownloaf form.