VBA从网站使用变量名

时间:2019-06-14 09:31:03

标签: html vba web-scraping

我正在努力下载带有时间戳的每日更新文件,非常感谢任何帮助

我想引用标签名称或其他名称,这样就不需要包含时间戳记

'Create InternetExplorer Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "https://my.euroclear.com/content/dam/euroclear/synchronization/eui_growth_%20market_stamp_exemption/GrowthMarkets_StampExemption_13062019_093138.xlsx": Application.Wait Now + TimeValue("00:00:02")
Application.SendKeys "%{S}", True

也许是这样吗?

我认为类似这样的方法应该可以工作,但是我无法使它工作:

Sub File() 
Const URL = "my.euroclear.com/eui/en/reference/public/…" 
Dim http As New XMLHTTP60, html As New HTMLDocument, post As Object, i As Integer 

With http .Open "GET", URL, False .send 
html.body.innerHTML = .responseText 
End 

With For Each post In html.getElementsByClassName("downloads")(0).getElementsByTagName("li") 

    With post.getElementsByTagName("a") 
        If .Length Then i = i + 1: Cells(i, 1) = .Item(0).innerText 
    End With 

Next post 

End Sub

2 个答案:

答案 0 :(得分:0)

尝试下面的代码

据我所知,您必须使用整个URL,否则VBA将不知道从何处下载。一种选择是使链接动态化,并使用少量用户输入作为时间戳(可能还有日期)

我利用了一个输入框,以便用户可以输入文件的时间戳。我希望这有助于使文件下载更加动态。对于日期,使用当前日期。您当然可以将其更改为另一个日期。

Dim URL As String
Dim Extension As String
Dim TimeStamp As Variant

URL = "https://my.euroclear.com/content/dam/euroclear/synchronization/eui_growth_%20market_stamp_exemption/GrowthMarkets_StampExemption_"
Extension = ".xlsx"
DayStamp = Format(Date, "ddmmyyyy") & "_"
TimeStamp = InputBox("Enter the file's timestamp in hhmmss")

'Create InternetExplorer Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate URL & DayStamp & TimeStamp & Extension

Application.Wait Now + TimeValue("00:00:02")
Application.SendKeys "%{S}", True

答案 1 :(得分:0)

已解决:

Dim downloadlink As Variant
Dim i As Integer
Dim IE As Object

'Create InternetExplorer Object
 Set IE = CreateObject("InternetExplorer.Application")
 IE.Visible = True: IE.Navigate "https://my.euroclear.com/eui/en/reference/public/growth-market-stamp-exemption.html": While IE.Busy: DoEvents: Wend: Application.Wait Now + TimeValue("00:00:02")

'Downloads all Reports
Set downloadlink = Nothing
i = 0
While i < IE.document.Links.Length And downloadlink Is Nothing
    If Left(IE.document.Links(i).innerText, 13) = "Current data:" Then 
    IE.document.Links(i).Click: Application.Wait Now + TimeValue("00:00:02"): 
    Application.SendKeys "%{S}", True
    i = i + 1
Wend
IE.Quit
Set IE = Nothing

End Sub