Python查询天气小程序

输入城市名,打印查询结果

#!/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"今明两天的天气状况是:\n %s %s; %s %s;\n"%\
				(weather_of_today,temperature_of_today,
				weather_of_tomorrow,temperature_of_tomorrow)
		weatherTips = u"友情提示:\n%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)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值