python - Wunderground api search bar -


i'm writting program search weather. i'm trying create option can search location doesn't seem working.

  urllib.request import request, urlopen   import json   import re   location = input('location know weather for')  api_key = '<api-key>' url = 'http://python-weather-api.googlecode.com/svn/trunk/ python-weather-api' + api_key +'/geolookup/conditions/q/ia/'+ location +'.json'   response = urllib.request.request(url)   def response request(url) json_string = response.read().decode('utf8') parsed_json = json.loads(json_string) location = parsed_json['location']['city']   temp_f = parsed_json['current_observation']['temp_f'] print("current temperature in %s is: %s" % (location, temp_f)) response.close() 

i keep on recieving error

can't comment yet (reputation low since joined so), regarding "urllib not defined" issue, has how import urlopen function.

instead of:

urllib.urlopen(url) 

try:

urlopen(url) 

edit: here's code, fixed:

from urllib.request import urlopen import json  location = input('location know weather for')  api_key = '<api-key>' url = 'http://api.wunderground.com/api/' + api_key + '/geolookup/conditions/q/ia/'+ str(location) +'.json'  response = urlopen(url)  json_string = response.read().decode('utf8') parsed_json = json.loads(json_string) location = parsed_json['location']['city'] temp_f = parsed_json['current_observation']['temp_f'] print("current temperature in %s is: %s" % (location, temp_f)) 

works fine tama , other cities in ia. watch out though, place names des moines won't work, because spaces aren't allowed in urls - you'll have take care of that. (the example api suggests _ spaces http://www.wunderground.com/weather/api/d/docs?mr=1). luck!


Comments