【发布时间】:2019-07-21 05:36:36
【问题描述】:
在 Sheet1 中,我在 B1 中有代码(比如“AA”),在 B2 中有开始日期(比如 2009 年 4 月 21 日),在 B3 中有结束日期(比如 2009 年 4 月 23 日)
当手动导航到所需的网址时,我得到了这样的链接 https://finance.yahoo.com/quote/AA/history?period1=1240290000&period2=1240462800&interval=1d&filter=history&frequency=1d
但是当使用代码来构建链接时,我得到的 UNIX 时间戳略有不同,如下所示 https://finance.yahoo.com/quote/AA/history?period1=1240272000&period2=1240444800&interval=1d&filter=history&frequency=1d
Notice period1 例如在两个链接中 如何调整代码以与雅虎的链接相同?
我尝试过类似的方法
period1 = ToUnix(.Range("B2").Value & " 05:00:00")
这解决了这些日期的问题,但没有解决其他不同的日期,所以我的逻辑不正确
这是我尝试过的代码
Sub Yahoo_Finance()
Dim ws As Worksheet
Dim sURL As String
Dim sTicker As String
Dim period1 As Long
Dim period2 As Long
Dim r As Long
Set ws = ThisWorkbook.Worksheets("Sheet1")
r = 6
With CreateObject("MSXML2.ServerXMLHTTP")
With ws
sTicker = .Range("B1").Value
period1 = ToUnix(.Range("B2").Value & " 05:00:00")
period2 = ToUnix(.Range("B3").Value & " 05:00:00")
End With
sURL = "https://finance.yahoo.com/quote/" & sTicker & "/history?period1=" & period1 & "&period2=" & period2 & "&interval=1d&filter=history&frequency=1d"
Debug.Print sURL
End With
End Sub
Public Function ToUnix(dt) As Long
ToUnix = DateDiff("s", "1/1/1970", dt)
End Function
【问题讨论】:
-
我很困惑。你是说在上面你现在使用你的 ToUnix 函数得到匹配的 url,但是其他 url 结构有问题?
-
如果你省略“05:00:00”部分,你会得到不同的url。我的意思是在使用按日期过滤时,我需要获得与浏览器中相同的 url。尝试其他旧日期并注意浏览器上的 url 和在我的代码中构造的 url 没有这部分“05:00:00”
标签: excel vba yahoo-finance