urllib-learn1.py 文件源码

python
阅读 37 收藏 0 点赞 0 评论 0

项目:learn-python 作者: xushubo 项目源码 文件源码
def fetch_xml(url):
    with request.urlopen(url) as f:
        print('Status:', f.status, f.reason)
        for k, v in f.getheaders():
            print('%s: %s' % (k, v))
        html = f.read().decode('utf-8')
    pattern_one = re.compile(r'<yweather:location.*?city="(.*?)".*?country="(.*?)".*?region="(.*?)".*?/>', re.S)
    pattern_two = re.compile(r'<yweather:forecast.*?date="(.*?)".*?day="(.*?)".*?high="(.*?)".*?low="(.*?)".*?text="(.*?)".*?/>', re.S)
    location_info = re.findall(pattern_one, html)
    items = re.findall(pattern_two, html)
    weather = {}
    weather['city'] = location_info[0][0]
    weather['country'] = location_info[0][1]
    weather['region'] = location_info[0][2]
    for item in items:
        weather[item[1]] = {}
        weather[item[1]]['data'] = item[0]
        weather[item[1]]['high'] = item[2]
        weather[item[1]]['low'] = item[3]
        weather[item[1]]['text'] = item[4]
    return weather
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号