-
Notifications
You must be signed in to change notification settings - Fork 0
/
forecast.py
42 lines (34 loc) · 1005 Bytes
/
forecast.py
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
38
39
40
41
42
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import urllib2
from bs4 import BeautifulSoup
def main(url):
symbols_dict = {
'clear sky': 'solsken',
'fair': 'sol och lite moln',
'cloudy': 'molnigt',
'partly cloudy': 'lite molnigt',
'light rain': 'lätt regn',
'rain showers': 'regnskurar',
'showers': 'regnskurar',
'heavy rain showers': 'kraftigt regnfall',
'light rain showers': 'lätta regnskurar',
}
try:
xml = urllib2.urlopen(url)
data = xml.read()
xml.close()
except:
print('Could not open URL: ' + url)
return None
soup = BeautifulSoup(data, 'lxml')
forecast = soup.forecast.tabular.find('time') # closest time
symbol_name = forecast.symbol['name'].lower()
if symbol_name in symbols_dict:
return (symbol_name, symbols_dict[symbol_name])
else:
return None
def get(url):
return main(url)
if __name__ == '__main__':
main()