Weather Scraper: 5 Day Forecast
Posted: Sat Apr 22, 2006 5:59 pm
Find below a script that will pull the 5 day forecast for your area from NOAA's mobile users website (US only):
Osler
Code: Select all
'Step 1: Create a Script Device called "WeatherForecast"
'Step 2: Go to NOAA's website (http://mobile.srh.weather.gov/), enter your zip code, and select "Detailed 7-day Forecast"
'Step 3: Cut and Paste the web address from the top of the "Detailed 7-day Forecast" page into the sURLCurrent line below
'Step 4: Save the script into the HouseBot Script file
'Step 5: Click on the "WeatherForecast" Script Device and point it to the script you just saved
'Step 6: Run the script from the Script Device - a message box will appear initially showing you the results (you can do this prior to adding all of the properties above to ensure it works correctly)
'Step 7: Open the "Properties Manager" and add Date1 through Date10 and DateForecast1 through DateForecast10
'===Name and Description should be the same
'Step 8: Add the properties you added to the script device
'Step 9: Read through the comments to remove the MsgBox debugging code (i.e., add a ' in front of it)
'Step 10: Read through the comments to activate the code to write the variables to the device properties (i.e., remove the ' in front of SetPropertyValue lines)
'This is a script to scrape current weather information from your local observation site
Dim sCurrent, oXMLHTTP, sURLCurrent, Parse1, Parse2
Dim sDate(9), sDateForecast(9)
Dim sDateOneline(9)
'Enter the corresponding web address for your area into the following variables from the website as described in Step 3 above
sURLCurrent = "http://www.srh.noaa.gov/port/port_mp_ns.php?select=2&CityName=Flower%20Mound&site=FWD&State=TX&warnzone=TXZ103"
'Create the xmlhttp object so you can use it to gather data from the webpages
Set oXMLHTTP = CreateObject("microsoft.xmlhttp")
'Get the current XML data from the Observation Location
oXMLHTTP.Open "GET", sURLCurrent, True
oXMLHTTP.Send
'Do some error trapping to ensure the server is up and all data was sent
If oXMLHTTP.readyState = 4 Then
If oXMLHTTP.status = 200 Then
'Save the MXL from the site as text so you can manipulate it as a string
sCurrent = oXMLHTTP.responseText
Else
alert("There was a problem retrieving the current weather data.")
End If
End If
'Destroy the object you created
Set oXMLHTTP = Nothing
'Now parse the data out of the file
Parse1 = 1
'Search for the days listed and the forecasts for those days
'Though a 7-day forecast is given, only 5 days are parsed
For I = 0 to 9
sDate(I) = ""
sDateForecast(I) = ""
Parse1 = InStr(Parse1, sCurrent, "<b>")
Parse1 = Parse1 + 3
Parse2 = InStr(Parse1, sCurrent, "</b>")
sDate(I) = Trim(Mid(sCurrent, Parse1, Parse2 - Parse1))
Parse1 = Parse2 + 7
Parse2 = InStr(Parse1, sCurrent, "<br><br>")
sDateForecast(I) = Trim(Mid(sCurrent, Parse1, Parse2 - Parse1))
sDateForecast(I) = Replace(sDateForecast(I), Chr(10), " ")
'Add a ' in front of MsgBox when you are satisfied the script is working correctly
MsgBox sDate(I) & vbCR & sDateForecast(I)
'Remove the ' from in front of all the SetPropertyValue below to have the script populate the device properties
Select Case I
Case 0
'SetPropertyValue "WeatherForecast.Date1", sDate(I)
Case 1
'SetPropertyValue "WeatherForecast.Date2", sDate(I)
Case 2
'SetPropertyValue "WeatherForecast.Date3", sDate(I)
Case 3
'SetPropertyValue "WeatherForecast.Date4", sDate(I)
Case 4
'SetPropertyValue "WeatherForecast.Date5", sDate(I)
Case 5
'SetPropertyValue "WeatherForecast.Date6", sDate(I)
Case 6
'SetPropertyValue "WeatherForecast.Date7", sDate(I)
Case 7
'SetPropertyValue "WeatherForecast.Date8", sDate(I)
Case 8
'SetPropertyValue "WeatherForecast.Date9", sDate(I)
Case 9
'SetPropertyValue "WeatherForecast.Date10", sDate(I)
End Select
Select Case I
Case 0
'SetPropertyValue "WeatherForecast.DateForecast1", sDateForecast(I)
Case 1
'SetPropertyValue "WeatherForecast.DateForecast2", sDateForecast(I)
Case 2
'SetPropertyValue "WeatherForecast.DateForecast3", sDateForecast(I)
Case 3
'SetPropertyValue "WeatherForecast.DateForecast4", sDateForecast(I)
Case 4
'SetPropertyValue "WeatherForecast.DateForecast5", sDateForecast(I)
Case 5
'SetPropertyValue "WeatherForecast.DateForecast6", sDateForecast(I)
Case 6
'SetPropertyValue "WeatherForecast.DateForecast7", sDateForecast(I)
Case 7
'SetPropertyValue "WeatherForecast.DateForecast8", sDateForecast(I)
Case 8
'SetPropertyValue "WeatherForecast.DateForecast9", sDateForecast(I)
Case 9
'SetPropertyValue "WeatherForecast.DateForecast10", sDateForecast(I)
End Select
Next