本帖最后由 凌风清羽 于 2016-1-25 20:02 编辑  
 
1.获取天气 
vi weather.py 
 
			
			
			- #!/usr/bin/python
 - # coding: utf-8
 - #create by Renqiang  ,renqiang@xiaokr.cc
 - import sys,getopt
 - import urllib
 - from xml.dom import minidom
 - WEATHER_URL = 'http://xml.weather.yahoo.com/forecastrss?w=%s'
 - WEATHER_NS =  'http://xml.weather.yahoo.com/ns/rss/1.0'
 - 
 - #define Yahoo WebService Function
 - def weather_for_zip(zip_code):
 -         url = WEATHER_URL % zip_code
 -          #download data by urllib and then use minidom transport text to XML
 -         dom = minidom.parse(urllib.urlopen(url))
 -         forecasts = []
 -           #find the keywords from weather forecast in XML by  recycling
 -         for node in dom.getElementsByTagNameNS(WEATHER_NS,'forecast'):
 -                forecasts.append({
 -                            'date':node.getAttribute('date'),
 -                            'low':node.getAttribute('low'),
 -                            'high':node.getAttribute('high'),
 -                            'condition':node.getAttribute('text')
 -                            })
 -         #find the keywords of now weather condition in XML                                   
 -         ycondition = dom.getElementsByTagNameNS(WEATHER_NS,'condition')[0]
 -         return{
 -                'current_condition':ycondition.getAttribute('text'),
 -                'current_temp':ycondition.getAttribute('temp'),
 -                'forecasts':forecasts,
 -                'title':dom.getElementsByTagName('title')[0].firstChild.data
 -               }
 - #city ID  
 - #Shanghai 2151849 Beijing 2151330
 - zipcode = 2151849
 - #        if(len(sys.argv)>0):
 -  #          zipcode = sys.argv[1]
 - print weather_for_zip(zipcode)
 
  复制代码 温度单位默认为华摄氏度WEATHER_URL = 'http://xml.weather.yahoo.com/forecastrss?w=%s&u=c' 
加入u=c单位变为摄氏度 
 输出不够优雅,大神可以帮忙改一下~~~~~~~~O(∩_∩)O~~ 
 
 
 |