天气预报(Python3.4 re和urllib2的使用)

来源:互联网 发布:js window.android 编辑:程序博客网 时间:2024/04/28 07:52
import urllib.request as requestimport redef getHtml(url):    html= request.urlopen(url).read().decode()    return htmldef getweather(html):    try:        weatherlist1=re.compile(r'<a title="(.+?)天气预报"').findall(html)        weatherlist2=re.compile(r'target="_blank"><img alt="(.+?)"').findall(html)        weatherlist3=re.compile(r'target="_blank"><span>(.+?)</span></a>/<a ').findall(html)        weatherlist4=re.compile(r'target="_blank"><b>(.+?)</b').findall(html)        time=re.compile(r'span="4">(.+?)的预报<').findall(html)        print(time[0])        for i in (range(0,len(weatherlist1))):            print(weatherlist1[i],'   ',weatherlist2[i],'    ',weatherlist3[i],'/',weatherlist4[i])    except:        print('获取失败,请重试!')url='http://henan.weather.com.cn/index.shtml'a=getHtml(url)a=getweather(a)


0 0
原创粉丝点击