2017-07-12 3 views
0

Ich verwende this link als Referenz zum Herunterladen einer ZIP-Datei von URL.Verwenden Sie VBA zum Herunterladen von Zip-Datei von URL

Der Code ich verwende ist in unter

Sub DownloadZipExtractCsvAndLoad() 
    Dim UrlFile As String, ZipFile As String, CsvFile As String, Folder As String, s As String 
    ' UrlFile to the ZIP archive 
    UrlFile = "https://loanperformancedata.fanniemae.com/lppub/publish?file=2008Q1.zip" 
    ' Extract ZipFile from UrlFile 
    ZipFile = "2008Q1.zip" 
    ' Define temporary folder 
    Folder = "C:\Users\xxxxxx\Desktop\" 
    ' Disable screen updating to avoid blinking 
    Application.ScreenUpdating = False 
    ' Trap errors 
    On Error GoTo exit_ 
    ' Download UrlFile to ZipFile in Folder 
    If Not Url2File(UrlFile, Folder & ZipFile, "xxx", "xxxx") Then 
    MsgBox "Can't download file" & vbLf & UrlFile, vbCritical, "Error" 
    Exit Sub 
    End If 
exit_: 
    ' Restore screen updating 
    Application.ScreenUpdating = True 
    ' Inform about the reason of the trapped error 
    If Err Then MsgBox Err.Description, vbCritical, "Error" 
End Sub 

Function Url2File(UrlFile As String, PathName As String, Optional Login As String, Optional Password As String) As Boolean 
'ZVI:2017-01-07 Download UrlFile and save it to PathName. 
'    Use optional Login and Password if required. 
'    Returns True on success downloading. 
    Dim b() As Byte, FN As Integer 
    On Error GoTo exit_ 
    If Len(Dir(PathName)) Then Kill PathName 
    With CreateObject("MSXML2.XMLHTTP") 
    .Open "GET", UrlFile, False, Login, Password 
    .send 
    If .Status <> 200 Then Exit Function 
    b() = .responseBody 
    FN = FreeFile 
    Open PathName For Binary Access Write As #FN 
    Put #FN, , b() 
exit_: 
    If FN Then Close #FN 
    Url2File = .Status = 200 
    End With 
End Function 

jedoch nur jedes Mal, wenn ich den Code ausführen, wird es eine leere Zip-Datei statt dem Herunterladen der Datei erstellen.

Irgendwelche Hilfe?

+0

Wenn Sie zu der tatsächlichen URL gehen, die Sie abrufen möchten (z. B. einfügen ** https: //loanperformancedata.fanniemae.com/lppub/publish? file = 2008Q1.zip ** in die Suchleiste Ihres Browsers), Sie werden sehen, dass die Datei nicht existiert. – ainwood

+0

@ainwood Ich bin neu in diesem Bereich. Diese Website benötigt Login-Informationen. Nachdem ich mich mit meinem Benutzernamen und meinem Passwort angemeldet habe, funktioniert der Link. – kzhang12

Antwort

-1

Ich gehe davon aus, dass Sie in der Lage sind, die Datei zu erhalten, indem Sie einen Webbrowser und die Protokollierung in

mit ihm die b() in der Nähe der unteren

es sein sollte:

b = fileObj.responseBody 
. 
. 
Put #FN, , b 

Ich testete es durch Abrufen UrlFile = "https://www.google.ca/"

Ich habe ein paar Zeilen hinzugefügt, um den Status zu drucken nach dem Dateiabrufversuch

Sub DownloadZipExtractCsvAndLoad() 

    Dim UrlFile As String, ZipFile As String, CsvFile As String, Folder As String, s As String 


    UrlFile = "https://loanperformancedata.fanniemae.com/lppub/publish?file=2008Q1.zip" ' UrlFile to the ZIP archive 
    ZipFile = "2008Q1.zip"                 ' Extract ZipFile from UrlFile 

    UrlFile = "https://www.google.ca/"     ' debug ... test url 
    ZipFile = "2008Q1.html"        ' debug ... test file 

    Folder = "C:\Users\js135001\Desktop\"             ' Define temporary folder 
    Application.ScreenUpdating = False              ' Disable screen updating to avoid blinking 

' On Error GoTo exit_err                ' Trap errors 

    If Not Url2File(UrlFile, Folder & ZipFile, "xxx", "xxxx") Then       ' Download UrlFile to ZipFile in Folder 
     MsgBox "Can't download file" & vbLf & UrlFile, vbCritical, "Error" 
     Exit Sub 
    End If 

exit_err: 
    Application.ScreenUpdating = True              ' Restore screen updating 

    If Err Then MsgBox Err.Description, vbCritical, "Error"        ' Inform about the reason of the trapped error 

End Sub 

Function Url2File(UrlFile As String, PathName As String, Optional Login As String, Optional Password As String) As Boolean 
    ' ZVI:2017-01-07 Download UrlFile and save it to PathName. 
    '    Use optional Login and Password if required. 
    '    Returns True on success downloading. 

    Dim b() As Byte, FN As Integer 

' On Error GoTo exit_err 

    If Len(Dir(PathName)) Then Kill PathName 

    Dim httpObj As Object 
    Set httpObj = CreateObject("MSXML2.XMLHTTP") 

    httpObj.Open "GET", UrlFile, False, Login, Password 
    httpObj.send 

    Debug.Print httpObj.Status    ' debug 
    Debug.Print httpObj.statusText   ' debug 

    If httpObj.Status <> 200 Then Exit Function 

    b = httpObj.responseBody 
    FN = FreeFile 
    Open PathName For Binary Access Write As #FN 
    Put #FN, , b 

' Put #FN, , httpObj.responseBody ' you could do this, and not use b() at all 

exit_err: 
    If FN Then Close #FN 
    Url2File = (httpObj.Status = 200)    ' return true/false 

End Function 
+0

Wenn ich Ihren Code testen, ist der httpObj.Status immer 404. Ich bin nicht sicher, ob es durch die URL verursacht wird oder nicht. 'loanperformancedata.fanniemae.com/lppub/publish?file=2008Q1.zip' ist der echte Download-Link. Nachdem ich zu "https: // loanperformancedata.fanniemae.com/lppub/index.html" gegangen bin und mich angemeldet habe, funktioniert dieser Link. Andernfalls wird es sagen, dass die Datei nicht existiert. – kzhang12

+0

@ kzhang12, diese Webseite ist passwortgeschützt (benutze einen Webbrowser und du wirst sehen), deshalb habe ich einen Link zu _https: //www.google.ca/_ zum Testen hinzugefügt. Kommentieren Sie einfach die erste _ "UrlFile =" _ Zeile zum Testen. Ich weiß nicht, wie man ein Passwort zu der URL hinzufügt, die eine 404 zurückgibt. – jsotola

Verwandte Themen