Python 날씨 검색 애플 릿

1379 단어 pythonweather
도시 이름 입력,검색 결과 인쇄
#!/usr/bin/env python
#encoding:utf-8
#By eathings

import urllib
import urllib2
import re
from xml.dom.minidom import parseString

class Weather:
	def __init__(self):
		self.url = "http://www.webxml.com.cn/webservices/weatherwebservice.asmx/getWeatherbyCityName"

	def get_weather(self,thecityname):
		url = self.url + "?thecityname=" + thecityname
		result = urllib2.urlopen(url).read()
		dom = parseString(result)
		strings = dom.getElementsByTagName("string")
		temperature_of_today = self.getText(strings[5].childNodes)
		weather_of_today = self.getText(strings[6].childNodes)
		temperature_of_tomorrow = self.getText(strings[12].childNodes)
		weather_of_tomorrow = self.getText(strings[13].childNodes)
		weather_tips = self.getText(strings[11].childNodes)
		weatherStr = u"          :
%s %s; %s %s;
"%\ (weather_of_today,temperature_of_today, weather_of_tomorrow,temperature_of_tomorrow) weatherTips = u" :
%s" %weather_tips print weatherStr print weatherTips def getText(self,nodelist): """ """ rc="" for node in nodelist: if node.nodeType==node.TEXT_NODE: rc=rc+node.data return rc weath = Weather() print " , " thecityname = raw_input() weath.get_weather(thecityname)

좋은 웹페이지 즐겨찾기