码迷,mamicode.com
首页 > 编程语言 > 详细

python 读取yahoo 天气api 获取北京天气情况

时间:2015-01-30 16:20:21      阅读:404      评论:0      收藏:0      [点我收藏+]

标签:

代码如下:

#!/usr/bin/env python
# coding=utf-8
import urllib2
import xml.dom.minidom
try:
    url="http://weather.yahooapis.com/forecastrss?u=c&w=2151330"
    request = urllib2.Request(url)
    response = urllib2.urlopen(request)
    print "Geting data from yahooapis...."
    data = response.read()
    #print data
    dom = xml.dom.minidom.parseString(data)

    root = dom.documentElement
    location = root.getElementsByTagName(‘yweather:location‘)
    city = location[0].getAttribute("city")
    weather_conditions=root.getElementsByTagName(‘yweather:condition‘)
    weather = weather_conditions[0].getAttribute(‘text‘)
    temp = weather_conditions[0].getAttribute(‘temp‘)
    print "City:",city
    print "weather:",weather
    print "temp:",temp
except Exception:
    print("there is something wrong")


python 读取yahoo 天气api 获取北京天气情况

标签:

原文地址:http://my.oschina.net/7shell/blog/373532

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!