views:

98

answers:

2
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

A: 

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.

nearlymonolith
I made certain improvements in my code as below:#rest of code sames = 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 nonenow below is the structure of xml file<current_conditions><condition data="Cloudy"/><temp_f data="97"/>
Harshit Sharma
it returns none although it has cloudy in it , dont know to update questions I am new here
Harshit Sharma
I think none is coming because tree.find is not implemented correctlyfollowing is the url http://www.google.com/ig/api?weather=london
Harshit Sharma
It is solved thanks for your help
Harshit Sharma
You people rock
Harshit Sharma
+1  A: 

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)
Adam Crossland
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 Sharma
@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?
Adam Crossland
ya I got that thanks It worked Thanks once again You people rock
Harshit Sharma