我有一个电子表格,其中包含 A 列中的城市,州。我想从当天到 B 列中获取 3 天的沉淀类型/值。我开始使用 openweathermap API,我发现我缺乏 XML 经验正在扼杀我。以下是我的开始方式,但我无法走得更远。我为使用节点和元素所做的任何尝试都没有成功。任何帮助,将不胜感激。
这是一个示例 XML http://api.openweathermap.org/data/2.5/forecast/daily?q=Boston+MA&mode=xml&units=metric&cnt=7的链接?
Option Explicit
Function CityForecast(City As String) As String
Dim StartingURL As String
Dim SecondaryURL As String
Dim FinalURL As String
Dim CorrectedCity As String
Dim objXML As Object
Dim objDOM As MSXML2.DOMDocument60
CorrectedCity = Replace(City, " ", "+")
CorrectedCity = Replace(City, ",", "+")
StartingURL = "http://api.openweathermap.org/data/2.5/forecast/daily?q="
SecondaryURL = "&mode=xml&units=metric&cnt=7"
FinalURL = StartingURL + CorrectedCity + SecondaryURL
Debug.Print FinalURL
Set objXML = CreateObject("MSXML2.XMLHTTP")
Set objDOM = New MSXML2.DOMDocument60
With objXML
.Open "GET", FinalURL, False
.setRequestHeader "Content-Type", "application/x-www-form-URLEncoded"
.send
objDOM.LoadXML .responseText
End With
End Function
Function CorrectedDate(WxDate As String) As String
Dim yr As Integer
Dim dy As Integer
Dim mnth As Integer
yr = Left(WxDate, 4)
mnth = Right(Left(WxDate, 7), 2)
dy = Right(WxDate, 2)
CorrectedDate = mnth & "/" & dy & "/" & yr
End Function