Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1124402
  • 博文数量: 241
  • 博客积分: 4385
  • 博客等级: 上校
  • 技术积分: 2383
  • 用 户 组: 普通用户
  • 注册时间: 2009-06-07 23:13
文章分类

全部博文(241)

文章存档

2013年(1)

2012年(8)

2011年(62)

2010年(109)

2009年(61)

分类: Python/Ruby

2010-12-24 17:33:53

 
")
                weather = patternweather.findall(html)
                
                patterntemperature = re.compile("")
                temperature = patterntemperature.findall(html)
    
                if len(date)== 0:
                        print"Cannot get city's weather you input"
                        return
                for i in range(len(date)):
                        print"%s"% date[i],"\t%s"% weather[i],"\t%s"% temperature[i]

if__name__=="__main__":
        weather = Weather()
        weather.getWeather(weather.getHtml(url))

#!/usr/bin/env python
# -*- coding:utf8 -*-

import urllib2
import re

provice=raw_input('输入省名(请使用拼音):')
city=raw_input("输入市名(请使用拼音):")
url = ""+provice+"/"+city+".htm"

class Weather():
        def __init__(self):
                pass
        
        def getHtml(self, url):
                req = urllib2.Request(url)
                res = urllib2.urlopen(req)
                html = res.read().decode("gb2312").encode("utf8")
                res.close()
                
                return html
        
        def getWeather(self, html):
                patterndate = re.compile(">(\d{4}-\d{2}-\d{2} .+)<")
                date = patterndate.findall(html)
                
                patternweather = re.compile("
(.+)

([-]?\d{1,2}.+)

阅读(1855) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~