2014-12-25 126 views
-1

是否可以從提供下載按鈕的網站下載文件?我正在嘗試製作一個簡單的程序,用於下載Minecraft的紋理包並自動安裝它們。當提示輸入紋理包的URL時,用戶必須使用直接下載URL,這意味着在瀏覽器中右鍵單擊下載按鈕,然後單擊「複製鏈接位置」。這似乎比下載和手動安裝更復雜,因此可以使用下載頁面上提供的鏈接嗎?我可以通過下載鏈接從網站上下載文件嗎? Visual Basic

我不知道該怎麼做,但我有一個效率不高的方法 - 輸入URL,下載HTML頁面。然後掃描文件以獲得一個看起來像下載鏈接的字符串,並進行測試。如果它返回一些東西,那麼幸福的日子。如果沒有,它會在HTML文件中尋找另一個下載鏈接。

我想不出任何可行的東西,即使這似乎有點可疑。對不起,如果我聽起來天真,我是編程新手。

+1

如果你能夠從任何網站下載的紋理包,而不僅僅是特定的一個:這可能將是非常困難的,因爲有相當多沒有辦法爲你的應用程序知道你的下載。按照您的建議進行操作可能會有效,但問題在於,如果在該頁面上的紋理包之前還有更多下載,則可能會找到其他下載。由於紋理包位於.zip文件中,因此它會檢查鏈接是否以「.zip」結尾,或者鏈接的最後部分(最後一個「/」後面的所有內容)是否包含「.zip」。請注意,某些地址可能以「/blablabla.zip?forcedownload」結尾。 –

+0

好的,如果除了我建議的方法之外沒有其他方法,那麼我將如何去編碼? –

+0

對不起,沒有回覆你,但我還沒有時間坐下來爲你組裝一個工作代碼。明天我會回覆你的密碼。 :) –

回答

0

好的。所以這裏有一個可能的解決方案。

不幸的是,它只適用於鏈接完成。 (以http://開頭......)

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>[\[email protected]][\w.:@]+)\/?[\w\.?=%&=\[email protected]/$,]*" '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.

+0

你好,先生,是個天才。非常感謝! –