如何实现python在xml标签之间查找值?
发布时间:2020-12-20 12:37:07 所属栏目:Python 来源:网络整理
导读:我正在使用谷歌网站检索天气信息,我想在 XML标签之间找到值.下面的代码给我一个城市的天气状况,但我无法获得其他参数,如温度,如果可能的话,解释代码中隐含的分离函数的工作: import urllibdef getWeather(city): #create google weather api url url = "htt
我正在使用谷歌网站检索天气信息,我想在
XML标签之间找到值.下面的代码给我一个城市的天气状况,但我无法获得其他参数,如温度,如果可能的话,解释代码中隐含的分离函数的工作:
import urllib 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() # extract weather condition data from xml string weather = s.split("<current_conditions><condition data="")[-1].split(""")[0] # if there was an error getting the condition,the city is invalid 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() 谢谢 解决方法
好吧,这里是 – 针对您的特定情况的非完整解析器解决方案:
import urllib def getWeather(city): ''' given city name or postal code,return dictionary with current weather conditions ''' url = 'http://www.google.com/ig/api?weather=' try: f = urllib.urlopen(url + urllib.quote(city)) except: return "Error opening url" s = f.read().replace('r','').replace('n','') if '<problem' in s: return "Problem retreaving weather (invalid city?)" weather = s.split('</current_conditions>')[0] .split('<current_conditions>')[-1] .strip('</>') wdict = dict(i.split(' data="') for i in weather.split('"/><')) return wdict 和使用的例子: >>> weather = getWeather('94043') >>> weather {'temp_f': '67','temp_c': '19','humidity': 'Humidity: 61%','wind_condition': 'Wind: N at 21 mph','condition': 'Sunny','icon': '/ig/images/weather/sunny.gif'} >>> weather['humidity'] 'Humidity: 61%' >>> print '%(condition)snTemperature %(temp_c)s C (%(temp_f)s F)n%(humidity)sn%(wind_condition)s' % weather Sunny Temperature 19 C (67 F) Humidity: 61% Wind: N at 21 mph PS.请注意,谷歌输出格式的一个相当微不足道的变化将打破这一点 – 比如他们是在标签或属性之间添加额外的空格或标签.他们避免减少http响应的大小.但如果他们这样做了,我们必须熟悉正则表达式和re.split() PPS. str.split(sep)的工作方式在文档中有解释,这里有一段摘录:使用sep作为分隔符字符串,返回字符串中单词的列表. … sep参数可以包含多个字符(例如,’1<><>>>’>’<>‘)返回[‘1′,’2′,’3’] ).所以’text1< tag> text2< / tag> text3′.split(‘< / tag>‘)给我们[‘text1< tag> text2′,’text3′],然后[0]选择第一个元素’ text1< tag> text2′,然后我们拆分并选取’text2′,其中包含我们感兴趣的数据.真的非常陈词滥调. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |