LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Blogs > peonuser
User Name
Password

Notices

Rate this Entry

First experience at using the Google api

Posted 11-19-2011 at 09:39 PM by peonuser
Updated 11-19-2011 at 11:55 PM by peonuser

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']
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.
Posted in Uncategorized
Views 287 Comments 0
« Prev     Main     Next »
Total Comments 0

Comments

 

  



All times are GMT -5. The time now is 07:43 AM.

Main Menu
Advertisement

My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration