阅读背景:

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

来源:互联网 
 
 
import urllib.request as request
import re
def getHtml(url):
    html= request.urlopen(url).read().decode()
    return html
def 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='https://henan.weather.com.cn/index.shtml'
a=getHtml(url)
a=getweather(a)
import urllib.request as request
import re
d



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: