简体   繁体   English

从链接下载 .csv

[英]Download .csv from a link

I'm trying to build a VBA code which has as input this calendar:我正在尝试构建一个 VBA 代码,该代码将此日历作为输入:
https://www.fxstreet.com/economic-calendar# https://www.fxstreet.com/economic-calendar#

In this link there exists the option to download it in format .csv.在此链接中,可以选择以 .csv 格式下载它。 For example this was the link of the download.例如,这是下载链接。 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%2CCountdown 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

I want to define a code in VBA based on it, changing that start date and end date according to my input in cell "A1" and "A2", but it's impossible due to the structure of the link (it doesn't finish in .csv).我想根据它在 VBA 中定义一个代码,根据我在单元格“A1”和“A2”中的输入更改开始日期和结束日期,但由于链接的结构,这是不可能的(它没有在.csv)。 If you go to section of downloads in your browser, and press the link, it won't download again, instead a message of error will appear.如果您在浏览器中转到下载部分,然后按链接,它将不会再次下载,而是会出现一条错误消息。 It just works when opening the first link and selecting the option to download- so, I can´t build a structure in VBA based on it.它只在打开第一个链接并选择下载选项时起作用 - 所以,我无法基于它在 VBA 中构建结构。

Does there exist a way that VBA can open the link and then "select" the option to download, or do you have another idea to download it using VBA?是否存在 VBA 可以打开链接然后“选择”下载选项的方法,或者您有其他想法使用 VBA 下载它吗?

I don't see any kind of CSV file in the link you posted, but this is one way you could do it with 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

Not great due to sendkeys but does download the CSV for the current period.由于 sendkeys 不是很好,但确实下载了当前时期的 CSV。 Setting dates seems to be a lot harder.设定日期似乎要困难得多。 Whilst entering custom dates ranges and clicking apply is easy, the values don't appear to be retained (manually or through code!).虽然输入自定义日期范围并单击应用很容易,但这些值似乎不会保留(手动或通过代码!)。 The only way values seem to be retained is if you actually make selections on the calendar itself.似乎保留值的唯一方法是您实际在日历本身上进行选择。 That then becomes a lot more finicky.然后变得更加挑剔。 I could address that in a new question if required.如果需要,我可以在一个新问题中解决这个问题。

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

References:参考:

  1. VBE > Tools > References and add a reference to Microsoft Internet Controls VBE > Tools > References 并添加对Microsoft Internet Controls的引用

Adjust the 'iTable' variable to the table number that you want to import (ie, 1, 2, 3, etc)将“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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM