Skip to content

Instantly share code, notes, and snippets.

@jleclanche
Created May 13, 2012 19:05
Show Gist options
  • Star 29 You must be signed in to star a gist
  • Fork 15 You must be signed in to fork a gist
  • Save jleclanche/2689784 to your computer and use it in GitHub Desktop.
Save jleclanche/2689784 to your computer and use it in GitHub Desktop.
Quick python weather script using Google's Weather API
#!/usr/bin/env python
# encoding: utf-8
import sys
from argparse import ArgumentParser
from xml.dom import minidom
try:
from urllib.request import urlopen
from urllib.parse import urlencode
except ImportError:
from urllib import urlopen, urlencode
API_URL = "http://www.google.com/ig/api?"
def main():
arguments = ArgumentParser(prog="weather")
arguments.add_argument("--unit", choices="CF", dest="unit", default="C", help="Which unit to display the temperatures in")
arguments.add_argument("location", nargs="+")
args = arguments.parse_args(sys.argv[1:])
for location in args.location:
url = API_URL + urlencode({"weather": location})
xml = urlopen(url).read()
doc = minidom.parseString(xml)
forecast_information = doc.documentElement.getElementsByTagName("forecast_information")[0]
city = forecast_information.getElementsByTagName("city")[0].getAttribute("data")
current_conditions = doc.documentElement.getElementsByTagName("current_conditions")[0]
temp = current_conditions.getElementsByTagName("temp_f" if args.unit == "F" else "temp_c")[0].getAttribute("data")
condition = current_conditions.getElementsByTagName("condition")[0].getAttribute("data")
wind_condition = current_conditions.getElementsByTagName("wind_condition")[0].getAttribute("data")
humidity = current_conditions.getElementsByTagName("humidity")[0].getAttribute("data")
indent = " "
print("Weather for {0}:".format(city))
print(indent + "{0}°{1}".format(temp, args.unit))
print(indent + condition)
print(indent + wind_condition)
print(indent + humidity)
if __name__ == "__main__":
main()
"""
adys@azura ~/src/scripts % ./weather.py London Bucharest "New York"
Weather for London:
14°C
Partly Cloudy
Wind: S at 12 mph
Humidity: 55%
Weather for Bucharest, Bucuresti:
14°C
Partly Cloudy
Wind: E at 16 mph
Humidity: 59%
Weather for New York, NY:
26°C
Clear
Wind: N at 6 mph
Humidity: 32%
adys@azura ~/src/scripts % ./weather.py "Los Angeles" --unit=F
Weather for Los Angeles, CA:
64°F
Haze
Wind: N at 0 mph
Humidity: 68%
"""
@belindacosta
Copy link

I am getting an error that:

weather:error:the following arguments are required:location

@BrainRTP
Copy link

BrainRTP commented Oct 9, 2016

root@novvvela:/home/bot# python weather.py London --unit=C
Traceback (most recent call last):
File "weather.py", line 44, in
main()
File "weather.py", line 25, in main
doc = minidom.parseString(xml)
File "/usr/lib/python2.7/xml/dom/minidom.py", line 1928, in parseString
return expatbuilder.parseString(string)
File "/usr/lib/python2.7/xml/dom/expatbuilder.py", line 940, in parseString
return builder.parseString(string)
File "/usr/lib/python2.7/xml/dom/expatbuilder.py", line 223, in parseString
parser.Parse(string, True)
xml.parsers.expat.ExpatError: syntax error: line 1, column 0

Copy link

ghost commented Oct 10, 2016

@clutch123
Copy link

I am getting this error for both of the websites:weather: error: the following arguments are required: location

@JAYASREE123
Copy link

good morining .
can any help to me ,to find previous weather data by using python coding.

@AMV007
Copy link

AMV007 commented Nov 18, 2018

Google weather api dead for now, you can remove this code )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment