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
评论列表
文章目录