1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
# -*- coding: utf-8 -*-
import sys
import urllib2
import re
PAGE = 'http://weather.yahooapis.com/forecastrss?w=615702&u=c'
LINK = re.compile('(?<=<link>)(.*?)(?=</link>)', re.DOTALL)
TEMP = re.compile('(?<=<div class="day-temp-current temp-f ">)(.*?)(?=<span class="unit">)', re.DOTALL)
MIN = re.compile('(?<=<div class="day-high-low">)(.*?)(?=° )', re.DOTALL)
MAX = re.compile('(?<=<span class="day-temp-low">)(.*?)(?=°</span>)', re.DOTALL)
def load_page(url):
try:
content = urllib2.urlopen(url).read()
except IOError as why:
print 'Erreur:', why
sys.exit()
return content
def get_weather_link():
page = load_page(PAGE)
lk = LINK.search(page).group(0)
return lk
def get_weather_info(lk):
page = load_page(lk)
current = TEMP.search(page).group(0).replace(u'°', u'°')
minima = MIN.search(page).group(0).replace('H', '')
maxima = MAX.search(page).group(0).replace('L', '')
print u'Température actuelle: %s, minima: %s°, maxima: %s°' %(current, minima, maxima)
if __name__ == '__main__':
link = get_weather_link()
get_weather_info(link) |
Partager