Using a simple Google API.
Preface: There is a computer programming language that is oriented towards new developers. That language is called Python. It is available for most every platform. (i.e. MSWindows, OS/X, BSD,. Linux and etc. Though there are no fancy graphics in what was done in this project, you could surely add that feature. Just wanted to show a bare bones project that anyone could use to start with.
One of the reasons I like to do page scraping is to get the data I need without having to take time to use a web browser such as Firefox and or a newsreader to get the specific data I need. Letting the computer get the data for me and making a summary file of the data I need is therefore letting the computer be my secretary and or research assistant.
Have been doing some page scraping, but i was not able to get the weather from their (Google) site. Then read they have it sort of hidden. Was able to get a page of the xml they use to show the data. Aha! Then found a short piece of code of how to use the api.
The xml shorted version (using http://www.google.com//ig/api?weather=huntsville) :
<xml_api_reply version="1">
...
<forecast_information>
<city data="Huntsville, AL"/>
<postal_code data="huntsville"/>
<latitude_e6 data=""/>
<longitude_e6 data=""/>
<forecast_date data="2011-11-18"/>
<current_date_time data="2011-11-18 21:50:00 +0000"/>
<unit_system data="US"/>
</forecast_information>
−
<current_conditions>
<condition data="Clear"/>
<temp_f data="55"/>
<temp_c data="13"/>
<humidity data="Humidity: 29%"/>
<icon data="/ig/images/weather/sunny.gif"/>
<wind_condition data="Wind: SE at 8 mph"/>
</current_conditions>
...
</xml_api_reply>
So then it was a matter of just plugging in variables. Almost cut and paste. Anything in single quotes was extracted from the xml sort of. I also make it so that you did not have to redo the code of a different location. This also could be done in a gui environment, but for simplicities sake that part was not included.
weather.py:
[code]
import sys
import pywapi
import string
google_result = pywapi.get_weather_from_google(sys.argv[1])
print "\nThe weather report for " + sys.argv[1] + " on " + google_result['forecast_information']['current_date_time'] + " in: " + google_result['forecast_information']['city'] + " \n"
print "Sky condition: " + string.lower(google_result['current_conditions']['condition'])
print "Temperature: " + google_result['current_conditions']['temp_f'] + "F"
print google_result['current_conditions']['humidity']
print google_result['current_conditions']['wind_condition']
[/code]
usage: python weather.py zipcode or python weather.py "city state"
$ python weather.py 10001
The weather report for 10001 on 2011-11-18 21:51:00 +0000 in: New York, NY
Sky condition: clear
Temperature: 44F
Humidity: 37%
Wind: N at 0 mph
$ _
Note: I had to install the pywapi
$ sudo apt-get pywapi
This was a lot easier than page scraping. Anyway enjoy.
One of the reasons I like to do page scraping is to get the data I need without having to take time to use a web browser such as Firefox and or a newsreader to get the specific data I need. Letting the computer get the data for me and making a summary file of the data I need is therefore letting the computer be my secretary and or research assistant.
Have been doing some page scraping, but i was not able to get the weather from their (Google) site. Then read they have it sort of hidden. Was able to get a page of the xml they use to show the data. Aha! Then found a short piece of code of how to use the api.
The xml shorted version (using http://www.google.com//ig/api?weather=huntsville) :
<xml_api_reply version="1">
...
<forecast_information>
<city data="Huntsville, AL"/>
<postal_code data="huntsville"/>
<latitude_e6 data=""/>
<longitude_e6 data=""/>
<forecast_date data="2011-11-18"/>
<current_date_time data="2011-11-18 21:50:00 +0000"/>
<unit_system data="US"/>
</forecast_information>
−
<current_conditions>
<condition data="Clear"/>
<temp_f data="55"/>
<temp_c data="13"/>
<humidity data="Humidity: 29%"/>
<icon data="/ig/images/weather/sunny.gif"/>
<wind_condition data="Wind: SE at 8 mph"/>
</current_conditions>
...
</xml_api_reply>
So then it was a matter of just plugging in variables. Almost cut and paste. Anything in single quotes was extracted from the xml sort of. I also make it so that you did not have to redo the code of a different location. This also could be done in a gui environment, but for simplicities sake that part was not included.
weather.py:
[code]
import sys
import pywapi
import string
google_result = pywapi.get_weather_from_google(sys.argv[1])
print "\nThe weather report for " + sys.argv[1] + " on " + google_result['forecast_information']['current_date_time'] + " in: " + google_result['forecast_information']['city'] + " \n"
print "Sky condition: " + string.lower(google_result['current_conditions']['condition'])
print "Temperature: " + google_result['current_conditions']['temp_f'] + "F"
print google_result['current_conditions']['humidity']
print google_result['current_conditions']['wind_condition']
[/code]
usage: python weather.py zipcode or python weather.py "city state"
$ python weather.py 10001
The weather report for 10001 on 2011-11-18 21:51:00 +0000 in: New York, NY
Sky condition: clear
Temperature: 44F
Humidity: 37%
Wind: N at 0 mph
$ _
Note: I had to install the pywapi
$ sudo apt-get pywapi
This was a lot easier than page scraping. Anyway enjoy.
Comments
Post a Comment