Yahoo财务中检索URL数据引用的URL问题

问题描述 投票:1回答:3

当我尝试从特定股票中检索报价时,来自Yahoo的URL不起作用。关于它的讨论有很多,但是,关于VBA宏似乎什么也没显示

Sub Get_Data()
Dim URL As String
Dim Ticker As String
Dim http As New WinHttpRequest
Dim sCotes As String
Dim Lignes
Dim Valeurs
Dim i As Long
Dim j As Long
Dim sLigne As String
Dim sValeur As String

Ticker = Range("Ticker")

URL = "https://query1.finance.yahoo.com/v7/finance/download/TECK?period1=1540456339&period2=1571992339&interval=1d&events=history&crumb=kjOZLFv6ch2"
http.Send
sCotes = http.ResponseText

MsgBox sCotes

Lignes = Split(sCotes, Chr(10))
For i = 1 To UBound(Lignes) 'until the end of the Lignes variable
  sLigne = Lignes(i)
  Valeurs = Split(sLigne, ",")
  For j = 0 To UBound(Valeurs) - 1
  Select Case j
  Case 0
  sValeur = DateSerial(CLng(Left(Valeurs(0), 4)), CLng(Mid(Valeurs(0), 6, 2)), CLng(Right(Valeurs(0), 2)))
  Case 5
  sValeur = CLng(Valeurs(5))
  Case Else
  sValeur = CDbl(Replace(Valeurs(j), ".", ","))
  End Select
  Range("A1").Offset(i, j) = sValeur
  Application.StatusBar = Format(Cells(i, 1), "Short Date")
  Next
Next
Application.StatusBar = False

End Sub

在步骤Http.send上的执行错误:“只有在调用Open方法之后才能调用此方法”

excel vba web-scraping get yahoo-finance
3个回答
0
投票

尝试替换此代码

URL = "https://query1.finance.yahoo.com/v7/finance/download/TECK?period1=1540456339&period2=1571992339&interval=1d&events=history&crumb=kjOZLFv6ch2"
http.Send

使用此代码:

set http = Server.Createobject("MSXML2.ServerXMLHTTP.6.0")
URL = "https://query1.finance.yahoo.com/v7/finance/download/TECK?period1=1540456339&period2=1571992339&interval=1d&events=history&crumb=kjOZLFv6ch2"
http.open "POST", URL, False
http.Send

错误非常清楚:您需要在open方法之前调用Send方法。同样,这将是一个POST请求。您可能还需要将这两行放在[[after open方法中:

http.setRequestHeader "Content-Type", "application/x-www-form-urlencoded" http.setRequestHeader "Content-Length", 0

0
投票
该问题与此处的问题-How can I send an HTTP POST request to a server from Excel using VBA?大约有99%重复。无论如何,错误很明显,因为.Send()方法只是发送一个完全空的Dim http As New WinHttpRequest对象。

为了使代码正常工作,请从重复的问题中复制示例并打印http.ResponseText

Sub TestMe() Dim http As Object Dim url As String Set http = CreateObject("MSXML2.ServerXMLHTTP.6.0") url = "https://query1.finance.yahoo.com/v7/finance/download/TECK?period1=1540456339&period2=1571992339&interval=1d&events=history&crumb=kjOZLFv6ch2" http.Open "POST", url, False http.Send MsgBox http.responsetext End Sub


0
投票
尝试发送之前,您需要使用“ open”方法,并且GET非常合适。但是,有几件事......

有一种更简单的方法。值得添加的标头是User-Agent,它可以减轻提供给缓存结果的负担。下面显示了如何在指定的时间段内从服务器获取json响应并写入Excel。注意:您需要将代码连接到URL中。您可能还应该测试服务器的响应代码以确保成功。

我使用jsonconverter.bas作为json解析器来处理响应。从here下载原始代码,并将其添加到名为JsonConverter的标准模块中。然后,您需要转到VBE>工具>引用>添加对Microsoft脚本运行时的引用。从复制的代码中删除最上面的属性行。

startDateendDate的值需要作为unix时间戳传递。 @TimWilliams编写了一个很好的函数toUnix,用于将Date转换为我使用的Unix here。我添加了自己的功能来管理相反的转化。

此方法避免使用任何基于会话的标识符,因此避免了无效的曲奇面包屑的问题。


VBA:

Option Explicit Public Sub GetYahooHistoricData() Dim ticker As String, ws As Worksheet, url As String, s As String Dim startDate As Long, endDate As Long Set ws = ThisWorkbook.Worksheets("Sheet1") ticker = ws.Range("ticker") 'Range A1. Above write out range endDate = toUnix("2019-10-27") startDate = toUnix("2018-10-25") url = "https://query1.finance.yahoo.com/v8/finance/chart/" & ticker & "?region=US&lang=en-US&includePrePost=false&interval=1d&period1=" & startDate & "&period2=" & endDate & "&corsDomain=finance.yahoo.com&.tsrc=finance" With CreateObject("MSXML2.XMLHTTP") .Open "GET", url, False .setRequestHeader "User-Agent", "Mozilla/5.0" .setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT" .send s = .responseText End With Dim json As Object Set json = JsonConverter.ParseJson(s)("chart")("result") Dim dates As Object, results(), rows As Object, adjClose As Object, r As Long, headers() headers = Array("date", "close", "volume", "open", "high", "low", "adjclose") Set dates = json(1)("timestamp") ReDim results(1 To dates.Count, 1 To UBound(headers) + 1) Set rows = json(1)("indicators")("quote")(1) Set adjClose = json(1)("indicators")("adjclose")(1)("adjclose") For r = 1 To dates.Count results(r, 1) = GetDate(dates(r)) results(r, 2) = rows("close")(r) results(r, 3) = rows("volume")(r) results(r, 4) = rows("open")(r) results(r, 5) = rows("high")(r) results(r, 6) = rows("low")(r) results(r, 7) = adjClose(r) Next With ws .Cells(3, 1).Resize(1, UBound(headers) + 1) = headers .Cells(4, 1).Resize(UBound(results, 1), UBound(results, 2)) = results End With End Sub Public Function GetDate(ByVal t As Variant) As String GetDate = Format$(t / 86400 + DateValue("1970-01-01"), "yyyy-mm-dd") End Function Public Function toUnix(ByVal dt As Variant) As Long toUnix = DateDiff("s", "1/1/1970", dt) End Function

示例前10行:

enter image description here
© www.soinside.com 2019 - 2024. All rights reserved.