[英]Download .csv from a link
我正在尝试构建一个 VBA 代码,该代码将此日历作为输入:
https://www.fxstreet.com/economic-calendar#
在此链接中,可以选择以 .csv 格式下载它。 例如,这是下载链接。 https://calendar.fxstreet.com/eventdate/?f=csv&v=2&timezone=Central+Standard+Time&rows=&view=range&start=20180909&end=20180915&countrycode=US&volatility=0&culture=en&columns=CountryCurrency%2CCount
我想根据它在 VBA 中定义一个代码,根据我在单元格“A1”和“A2”中的输入更改开始日期和结束日期,但由于链接的结构,这是不可能的(它没有在.csv)。 如果您在浏览器中转到下载部分,然后按链接,它将不会再次下载,而是会出现一条错误消息。 它只在打开第一个链接并选择下载选项时起作用 - 所以,我无法基于它在 VBA 中构建结构。
是否存在 VBA 可以打开链接然后“选择”下载选项的方法,或者您有其他想法使用 VBA 下载它吗?
我在您发布的链接中没有看到任何类型的 CSV 文件,但这是您可以使用 VBA 执行此操作的一种方法。
Sub Download()
Dim myURL As String
myURL = "http://www.asx.com.au/data/options_code_list.csv"
Dim WinHttpReq As Object
Dim ostream 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:\your_path_here\file.csv")
oStream.Close
End If
End Sub
由于 sendkeys 不是很好,但确实下载了当前时期的 CSV。 设定日期似乎要困难得多。 虽然输入自定义日期范围并单击应用很容易,但这些值似乎不会保留(手动或通过代码!)。 似乎保留值的唯一方法是您实际在日历本身上进行选择。 然后变得更加挑剔。 如果需要,我可以在一个新问题中解决这个问题。
Option Explicit
Public Sub GetInfo()
Dim IE As New InternetExplorer, calendar As Object, t As Date
Const WAIT_TIME_SECS As Long = 10
With IE
.Visible = True
.navigate "https://www.fxstreet.com/economic-calendar#"
While .Busy Or .readyState < 4: DoEvents: Wend
t = Timer
Do
DoEvents
If Timer - t > WAIT_TIME_SECS Then Exit Do
On Error Resume Next
Set calendar = .document.querySelector(".fa.fa-calendar")
On Error GoTo 0
Loop While calendar Is Nothing
If calendar Is Nothing Then Exit Sub
.document.querySelector("[fxs_csv]").Click
With Application
.Wait Now + TimeSerial(0, 0, 2)
.SendKeys "%{S}"
.Wait Now + TimeSerial(0, 0, 5)
End With
.Quit
End With
End Sub
参考:
将“iTable”变量调整为要导入的表号(即 1、2、3 等)
Sub HTML_Table_To_Excel()
Dim htm As Object
Dim Tr As Object
Dim Td As Object
Dim Tab1 As Object
'Replace the URL of the webpage that you want to download
'Web_URL = "https://en.wikipedia.org/wiki/List_of_U.S._states_and_territories_by_population"
Web_URL = "https://www.fxstreet.com/economic-calendar"
'Create HTMLFile Object
Set HTML_Content = CreateObject("htmlfile")
'Get the WebPage Content to HTMLFile Object
With CreateObject("msxml2.xmlhttp")
.Open "GET", Web_URL, False
.send
HTML_Content.body.innerHTML = .responseText 'this is the highlighted part for the error
End With
Column_Num_To_Start = 1
iRow = 1
iCol = 1
iTable = 1
'Loop Through Each Table and Download it to Excel in Proper Format
For Each Tab1 In HTML_Content.getElementsByTagName("table")
With HTML_Content.getElementsByTagName("table")(iTable)
For Each Tr In .Rows
For Each Td In Tr.Cells
Worksheets("Sheet1").Cells(iRow, iCol).Select
Worksheets("Sheet1").Cells(iRow, iCol) = Td.innerText
iCol = iCol + 1
Next Td
iCol = Column_Num_To_Start
iRow = iRow + 1
Next Tr
End With
Next Tab1
MsgBox "Process Completed"
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.