0
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

3
  • 2
    Can you give the exact error that you're getting? Commented Jun 17, 2010 at 18:01
  • stackoverflow.com/questions/3063319/… Same question. Commented Jun 17, 2010 at 18:04
  • File "weatxml.py", line 36, in <module> main() File "weatxml.py", line 32, in main weather = getWeather(city) File "weatxml.py", line 18, in getWeather tree=ET.parse(s) File "/usr/lib/python2.6/xml/etree/ElementTree.py", line 862, in parse tree.parse(source, parser) File "/usr/lib/python2.6/xml/etree/ElementTree.py", line 579, in parse source = open(source, "rb") IOError: [Errno 2] No such file or directory: '<?xml version="1.0"?><xml_api_reply version="1"><weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0" ><forecast_informa... Commented Jun 17, 2010 at 18:09

2 Answers 2

3

Instead of

tree=ET.parse(s)

try

tree=ET.fromstring(s)

Also, your path to the data you want is incorrect. It should be: weather/current_conditions/condition

This should work:

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.fromstring(s)

    current= tree.find("weather/current_conditions/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)
Sign up to request clarification or add additional context in comments.

3 Comments

thanks that helped a lot but current=tree.find statement returns none structure of xml is as follows: <current_conditions> <condition data="Cloudy"/> <temp_f data="97"/> <temp_c data="36"/> <humidity data="Humidity: 28%"/> <icon data="/ig/images/weather/cloudy.gif"/> <wind_condition data="Wind: N at 0 mph"/>
@Harshit, the XML you show in your comment is not the actual XML that comes back from Google. The code that I gave you will work. Are you feeding it pretend data?
ya I got that thanks It worked Thanks once again You people rock
0

I'll give the same answer here I did in my comment on your previous question. In the future, kindly update the existing question instead of posting a new one.

Original

I'm sorry - I didn't mean that my code would work exactly as you desired. Your error is because s is a string and parse takes a file or file-like object. So, "tree = ET.parse(f)" may work better. I would suggest reading up on the ElementTree api so you understand what the functions I've used above do in practice. Hope that helps, and let me know if it works.

3 Comments

I made certain improvements in my code as below: #rest of code same s = f.read() tree=ET.fromstring(s) current= tree.find("current_conditions/condition") print current #condition_data = current.get("data") #weather = condition_data weather = current if weather == "<?xml version=": return "Invalid city" #return the weather condition return weather #code is working but returns none now below is the structure of xml file <current_conditions> <condition data="Cloudy"/> <temp_f data="97"/>
it returns none although it has cloudy in it , dont know to update questions I am new here
I think none is coming because tree.find is not implemented correctly following is the url google.com/ig/api?weather=london

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.