python 天气预报

来源:互联网 发布:反美颜软件app 编辑:程序博客网 时间:2024/06/14 10:47
**# -*- coding: cp936 -*-**import urllib2import re#http://qq.ip138.com/tianqi/url = "http://qq.ip138.com/10/tianqi/"class Weather():        def __init__(self):                pass        def getHtml(self, url):                req = urllib2.Request(url)                res = urllib2.urlopen(req)                html = res.read()                res.close()                return html        def getWeather(self, html):                patteraddress=re.compile(r'<ul class="query-hd">(.*)</ul>')                address=patteraddress.findall(html)                p=re.compile(r'<li(.*?)</li>')                address1=p.findall(address[0])                #print address                len1=len(address1)                for i in xrange(len1):                    riqip=re.compile(r'<div class="date">(.*?)<font color="red">(.*?)</font></div>|<div class="date">(.*?)</div>')                    riqi=riqip.findall(address1[i])                    print '----------------------------------'                    if i==0:                        print (riqi[0][0]+riqi[0][1]).decode('utf8')                    else:                        print riqi[0][-1].decode('utf8')                    tianqip=re.compile(r'alt="(.*?)"')                    tianqi=tianqip.findall(address1[i])                    for j in tianqi:                        print j.decode('utf8')                    wendup=re.compile(r'<div class="temperature">(.*?)</div>')                    wendu=wendup.findall(address1[i])                    for i in wendu:                        print i.decode('utf8')                print '----------------------------------'if __name__ == "__main__":        weather = Weather()        weather.getWeather(weather.getHtml(url))        input()

“`