Can't parse XML effectively using Python
Posted
by Harshit Sharma
on Stack Overflow
See other posts from Stack Overflow
or by Harshit Sharma
Published on 2010-06-17T17:58:28Z
Indexed on
2010/06/17
18:03 UTC
Read the original article
Hit count: 346
import urllib
import xml.etree.ElementTree as ET
def getWeather(city):
#create google weather api url
url = "http://www.google.com/ig/api?weather=" + urllib.quote(city)
try:
# open google weather api url
f = urllib.urlopen(url)
except:
# if there was an error opening the url, return
return "Error opening url"
# read contents to a string
s = f.read()
tree=ET.parse(s)
current= tree.find("current_condition/condition")
condition_data = current.get("data")
weather = condition_data
if weather == "<?xml version=":
return "Invalid city"
#return the weather condition
#return weather
def main():
while True:
city = raw_input("Give me a city: ")
weather = getWeather(city)
print(weather)
if __name__ == "__main__":
main()
gives error , I actually wanted to find values from google weather xml site tags
© Stack Overflow or respective owner