从VBA网站下载所有带前缀的文件

时间:2018-07-24 08:42:15

标签: vba excel-vba web-scraping

我有一个网站,其中包含指向csv文件的100个链接,这些链接在单击后会自动下载。每个文件都有一个前缀aaa_形式。 以下标准代码允许您将基于URL的文件保存到磁盘上的选定位置:

Sub Download_from_website()

Dim myURL As String
myURL = "https://mysite/2500/csv/aaa_1.csv"

Dim WinHttpReq As Object
Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")
WinHttpReq.Open "GET", myURL, False
WinHttpReq.Send

myURL = WinHttpReq.ResponseBody
If WinHttpReq.Status = 200 Then
    Set oStream = CreateObject("ADODB.Stream")
    oStream.Open
    oStream.Type = 1
    oStream.Write WinHttpReq.ResponseBody
    oStream.SaveToFile ("C:\Users\tkp\Desktop\download_from_website\aaa_1.csv")
    oStream.Close
End If

End Sub

如何转换上面的代码,以便可以自动搜索网页以找到出现字符串aaa_的所有链接并将其自动保存在所选位置?我将不胜感激任何提示。

上面的代码是我想要获得的简化示例。但是,实际上,我想从站点保存所有带有SEB_前缀的文件 https://sebgroup.lu/private/luxembourg-based-funds/download-of-portfolio-holdings

2 个答案:

答案 0 :(得分:0)

这应该管理他们

{{1}}

答案 1 :(得分:0)

立即执行以下脚本。我想它将解决您现在遇到的问题。考虑到您想要所有链接中包含seb的csv文件这一事实,我编写了此脚本。

您在这里:

Sub DownloadFilesFromWeb()
    Const URL As String = "https://sebgroup.lu/private/luxembourg-based-funds/download-of-portfolio-holdings"
    Dim Http As New WinHttp.WinHttpRequest, Html As New HTMLDocument, I&, tempArr As Variant

    With Http
        .Open "GET", URL, False
        .send
        Html.body.innerHTML = .responseText
    End With

    With Html.querySelectorAll(".linklist a[href*='seb']")
        For I = 0 To .Length - 1
            tempArr = Split(.item(I).getAttribute("href"), "/")
            tempArr = tempArr(UBound(tempArr))

            Http.Open "GET", .item(I).getAttribute("href"), False
            Http.send

            With CreateObject("ADODB.Stream")
                .Open
                .Type = 1
                .write Http.responseBody
                ''notice the following line how the "tempArr" should be appended to the folder you have
                .SaveToFile "C:\Users\WCS\Desktop\downloadfile\" & tempArr
                .Close
            End With
        Next I
    End With
End Sub

要添加到库中的引用:

Microsoft HTML Object Library
Microsoft WinHTTP Services, version 5.1