天气预报查询器-python版

2009年10月9日  |  3:14 下午分类:python  |  标签:  |  

看了几天的python,写了个天气预报查询器玩玩

数据取自http://php.weather.sina.com.cn/

运行效果:

ScreenShot00051

主要用到了urllib2与re模块

urllib2模块来用发起一个HTTP GET请求,并获取响应。

re模块用正则匹配需要的内容。

代码写得比较烂,全当是熟悉语法了。

#coding=gb2312
import sys,urllib2,re
def GetWeather(city):
    url = 'http://php.weather.sina.com.cn/search.php?city='+city+'&dpc=1'
    response = urllib2.urlopen(url)
    result=response.read() #.decode('utf-8').encode("gbk")
    mathes = re.findall(r'<!-- box begin-->([\s\S]+?)<!-- box end-->',result)
    if len(mathes) == 0:
    	print '没有找到'
    	return
    data = []

    #找城市名
    cityname = re.findall(r'<div class="nav21">([\s\S]+?)<\/div>',result)
    if len(cityname) > 0:
    	city = filterHtmlTags(cityname[0])
    #天气
    try:
        data.append(re.findall(r'<h2>([\s\S]+?)<\/h2>',mathes[0])[0])
    except IndexError:
    	data.append('暂无数据')
    #温度
    try:
        data.append(re.findall(r'<div class="w-number">([\s\S]+?)<\/div>',mathes[0])[0])
    except IndexError:
    	data.append('暂无数据')
    #风向
    try:
        data.append(re.findall(r'<li>风向:([\s\S]+?)<\/li>',mathes[0])[0])
    except IndexError:
    	data.append('暂无数据')
    #风力
    try:
        data.append(re.findall(r'<li>风力:([\s\S]+?)<\/li>',mathes[0])[0])
    except IndexError:
    	data.append('暂无数据')
    #紫外线
    try:
        data.append(re.findall(r'<li>紫外线:([\s\S]+?)<\/li>',mathes[0])[0])
    except IndexError:
    	data.append('暂无数据')
    #舒适度
    try:
        data.append(re.findall(r'<li>舒适度:([\s\S]+?)<\/li>',mathes[0])[0])
    except IndexError:
    	data.append('暂无数据')
    #防晒指数
    try:
        data.append(re.findall(r'<li>防晒指数:([\s\S]+?)<\/li>',mathes[0])[0])
    except IndexError:
    	data.append('暂无数据')

    #明天
    t = []
    t.append(re.findall(r'<p>天气:([\s\S]+?)<\/p>',mathes[1])[0])
    t.append(re.findall(r'<p>温度:([\s\S]+?)<\/p>',mathes[1])[0])
    t.append(re.findall(r'<p>风力:([\s\S]+?)<\/p>',mathes[1])[0])
    data.append(t)
    #后天
    t = []
    t.append(re.findall(r'<p>天气:([\s\S]+?)<\/p>',mathes[2])[0])
    t.append(re.findall(r'<p>温度:([\s\S]+?)<\/p>',mathes[2])[0])
    t.append(re.findall(r'<p>风力:([\s\S]+?)<\/p>',mathes[2])[0])
    data.append(t)

    for a in range(len(data)):
    	data[a] = filterHtmlTags(data[a])

    result =  "城市:\t%s\n----今日天气----\n天气:\t%s\n温度:\t%s\n风向:\t%s\n风力:\t%s\n紫外线:\t%s\n舒适度:\t%s\n防晒指数:\t%s\n----明日天气----\n天气:\t%s\n温度:%s\n风力:%s\n----后天天气----\n天气:%s\n温度:%s\n风力:%s\n" % (city,data[0],data[1],data[2],data[3],data[4],data[5],data[6],data[7][0],data[7][1],data[7][2],data[8][0],data[8][1],data[8][2])
    print result

def filterHtmlTags(a):
	if isinstance(a,basestring) :
	    a = re.sub(r'<[^>]+>','',a)
	    a = re.sub(r'\s+','',a)
	if isinstance(a,list):
		for i in range(len(a)):
			a[i] = filterHtmlTags(a[i])
	return a
if __name__=="__main__":
	while True:
	    city = raw_input('请输入城市名 >>> ')
	    if len(city) == 0 :
		    print '退出'
		    sys.exit(0)
	    GetWeather(city)
转载时务必以超链接形式标明文章原始出处和作者信息。

相关文章

  • 暂无相关日志

10 位领导已批示 关于 “天气预报查询器-python版”

  1. 客串 发表于: 十月 9th, 2009 6:04 下午

    和我写的一样烂~

  2. 幻想曲 发表于: 十月 9th, 2009 6:07 下午

    @客串,
    囧~~~~

  3. 仁心博客 发表于: 十月 12th, 2009 5:20 下午

    向你学习了…………

  4. Dadait 发表于: 十月 14th, 2009 9:40 上午

    哇,都去搞Python了。

  5. leo 发表于: 十一月 3rd, 2009 11:49 上午

    蓝色的聚合blog.blueidea.com没了 真不好,找你的站都不好找。世界好小中午吃饭时候我同事竟然是你GF的以前同事,哈哈…

  6. 幻想曲 发表于: 十一月 3rd, 2009 2:13 下午

    1515
    这也太巧了~~~
    直接加收藏夹好了4

  7. 幻想曲 发表于: 十一月 3rd, 2009 2:16 下午

    @leo,
    也可以用我的网络收藏夹,记住一个地址就行了
    不过需要google account帐号登录
    http://yibinbookmark.appspot.com

  8. leo 发表于: 十一月 4th, 2009 10:48 上午

    @幻想曲, 以加收藏呵呵4

  9. Tonywoo 发表于: 四月 27th, 2010 5:55 下午

    我操!不好用!!!

  10. Tonywoo 发表于: 四月 27th, 2010 5:55 下午

    谁有好用的?发给我!!!


发表您的评论

1410168172122013183195114157619