如何使用VBScript从互联网上获取天气数据?

时间:2014-05-27 14:55:03

标签: vbscript weather

你可以在这里找到关于如何使用PHP,Ruby,Python,ColdFusion和jQuery获取天气数据的代码示例:http://www.wunderground.com/weather/api/d/docs?d=resources/code-samples,我想知道是否有人知道有关VBScript的相同内容的。

我对VB的理解非常有限,不过VBS,但我发现这个代码在网上似乎可能有用:

Option Explicit
'On Error Resume Next

Dim objXML, strXMLFile, weatherFor, weatherCondition, currTemp, highTemp, lowTemp

strXMLFile = "C:\Temp\test.xml"

Set objXML = CreateObject("Microsoft.XMLDOM")
objXML.async = "False"
objXML.load(strXMLFile)

weatherFor = objXML.getElementsByTagName("yweather:location").item(0).attributes.getNamedItem("city").value
weatherCondition = objXML.getElementsByTagName("yweather:condition").item(0).attributes.getNamedItem("text").value
currTemp = objXML.getElementsByTagName("yweather:condition").item(0).attributes.getNamedItem("temp").value
highTemp = objXML.getElementsByTagName("yweather:forecast").item(0).attributes.getNamedItem("high").value
lowTemp = objXML.getElementsByTagName("yweather:forecast").item(0).attributes.getNamedItem("low").value

WScript.Echo weatherFor
WScript.Echo weatherCondition
WScript.Echo currTemp
WScript.Echo highTemp
WScript.Echo lowTemp

但是当我尝试执行它时,我收到错误:Object required: getElementsByTagName(...).item(...)

我查了一下这就是问题所在:http://www.computerperformance.co.uk/Logon/code/code_800A01A8.htm,但我不知道该怎么做。

有人知道如何解决此错误代码,或者更好的方法来完成此操作吗?非常感谢你提前。

[编辑:]

这是一些有效的代码:

Dim WeatherRSS
WeatherRSS = "http://weather.yahooapis.com/forecastrss?p=CAXX0773&u=c"

Dim WinHttpReq, XMLData, objXML, weatherCity, weatherCondition, currTemp, highTemp, lowTemp
Dim Response, Talker

Set WinHttpReq = CreateObject("WinHttp.WinHttpRequest.5.1")

WinHttpReq.Open "GET", WeatherRSS, False
WinHttpReq.Send

If (WinHttpReq.Status = 200) Then
    XMLData = WinHttpReq.ResponseText

    Set objXML = CreateObject("Microsoft.XMLDOM")
    objXML.async = "False"
    objXML.loadXML(XMLData)

    weatherCity      = objXML.getElementsByTagName("yweather:location").item(0).attributes.getNamedItem("city").value
    weatherCondition = objXML.getElementsByTagName("yweather:condition").item(0).attributes.getNamedItem("text").value
    currTemp         = objXML.getElementsByTagName("yweather:condition").item(0).attributes.getNamedItem("temp").value
    highTemp         = objXML.getElementsByTagName("yweather:forecast").item(0).attributes.getNamedItem("high").value
    lowTemp          = objXML.getElementsByTagName("yweather:forecast").item(0).attributes.getNamedItem("low").value

    weather = weatherCondition & " " & currTemp & " " & highTemp & " " & 

lowTemp & " " + weatherCity & "."
else        weather = "Sorry, I can't predict the weather today."
end if

msgbox(weather)

0 个答案:

没有答案