-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrapper.py
233 lines (176 loc) · 6.2 KB
/
scrapper.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# -*- coding:utf-8 -*-
import re
import requests
from BeautifulSoup import BeautifulSoup
def slugify(value):
# from Django
"""
Normalizes string, converts to lowercase, removes non-alpha characters,
and converts spaces to hyphens.
"""
import unicodedata
value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore')
value = unicode(re.sub('[^\w\s-]', '', value).strip().lower())
return re.sub('[-\s]+', '-', value)
def air_now():
'''
List of indexes of airnow:
http://www.airnow.gov/index.cfm?action=airnow.national_summary
'''
url = 'http://www.airnow.gov/index.cfm?action=airnow.national_summary'
soup = BeautifulSoup(requests.get(url).content)
get_city = lambda tr: tr.find('a').getText()
get_index = lambda tr: tr.findChildren('td')[-3].getText()
trs = soup.findAll('tr', attrs={'bgcolor': re.compile('#EDF3F9|#FFFFFF')})
indexes = {}
for tr in trs:
city = get_city(tr)
indexes[slugify(city)] = (city, get_index(tr))
return indexes
def english_mep():
'''
List of indexes of english_mep:
http://english.mep.gov.cn/datarb/htm/index_1.html
'''
url = 'http://english.mep.gov.cn/datarb/htm/index_1.html'
soup = BeautifulSoup(requests.get(url).content)
trs = soup.findAll('tr', {'class': '.menu12'})
indexes = {}
for tr in trs:
city_name = tr.td.next
index = city_name.next.next
indexes[slugify(city_name)] = (city_name, index)
return indexes
def durban():
'''
Index of Durban:
http://www2.nilu.no/airquality/
'''
url = 'http://www2.nilu.no/airquality/'
soup = BeautifulSoup(requests.get(url).content)
span = soup.find('span', {'class': 'head1'})
span_value = span.next
city_name = span_value.split(' ')[-1]
bolds = soup.findAll('b')
index = bolds[1].next
return {slugify(city_name): (city_name, index)}
def hong_kong():
'''
Index of Hong Kong:
http://www.epd-asg.gov.hk/
'''
url = 'http://www.epd-asg.gov.hk/'
soup = BeautifulSoup(requests.get(url).content)
a = soup.find('a', {'href': '/english/backgd/Central.html'})
index = a.next.next.next.next.next.next
city_name = u'Hong Kong'
return {slugify(city_name): (city_name, index)}
def sydney():
'''
Indexes of Sydney:
http://airquality.environment.nsw.gov.au/aquisnetnswphp/getPage.php?repor
tid=3
'''
url = ('http://airquality.environment.nsw.gov.au/aquisnetnswphp/getPage.'
'php?reportid=3')
soup = BeautifulSoup(requests.get(url).content)
tds = soup.findAll('td', {'class': 'region'})
indexes = {}
for td in tds:
index = td.findNext(
'td', {'rowspan': re.compile('\d'), 'class': re.compile('\w')}
)
index = index.next
region = td.next
indexes[slugify(region)] = (region, index)
return indexes
def toronto():
'''
TODO: Get all indices of Ontario
http://www.airqualityontario.com/reports/summary.php
Index of Toronto:
http://www.airqualityontario.com/reports/today.php?sites=31103
'''
url = 'http://www.airqualityontario.com/reports/today.php?sites=31103'
soup = BeautifulSoup(requests.get(url).content)
index = soup.findAll('p')[2].getText()
index = re.search(r'\d+', index).group()
return {'toronto': ('Toronto', index)}
def san_juan():
'''
San Juan
http://www.prtc.net/%7Ejcaaqs/indice/indice.html
'''
url = 'http://www.prtc.net/~jcaagua/computoaire.htm' # iframe
soup = BeautifulSoup(requests.get(url).content)
index = soup.findAll('td')[3].getText()
return {'san-juan': ('San Juan', index)}
def air_quality():
'''
All indexes of airqualitynow.eu
http://www.airqualitynow.eu/comparing_home.php
'''
url = 'http://www.airqualitynow.eu/comparing_home.php'
soup = BeautifulSoup(requests.get(url).content)
cities_tds = soup.findAll('td', {'class': re.compile('city_bkg\d+')})
indexes = {}
for td in cities_tds:
index = td.parent.findAll('td')[-1] # background index: now
# Some kind of standart, empty string for null values
index = index.getText().replace('-', '')
indexes[slugify(td.getText())] = (td.getText(), index)
return indexes
def lisbon():
'''
Lisbon
http://www.qualar.org/INDEX.PHP?page=1&subzona=4
'''
url = 'http://www.qualar.org/INDEX.PHP?page=1&subzona=4'
soup = BeautifulSoup(requests.get(url).content)
td_base = soup.find('td', {'bgcolor': '#434343'})
index = td_base.parent.parent.find('center').getText()
return {'lisbon': ('Lisbon', index)}
def guimaraes():
'''
Guimarães
http://www.qualar.org/INDEX.PHP?page=1&zona=141
'''
url = 'http://www.qualar.org/INDEX.PHP?page=1&zona=141'
soup = BeautifulSoup(requests.get(url).content)
td_base = soup.find('td', {'bgcolor': '#434343'})
index = td_base.parent.parent.find('center').getText()
return {'guimaraes': (u'Guimarães', index)}
def santiago():
'''
Santiago (all zones)
http://www.seremisaludrm.cl/sitio/pag/aire/indexjs3aireindices-prueba.asp
'''
url = ('http://www.seremisaludrm.cl/sitio/pag/aire/'
'indexjs3aireindices-prueba.asp')
soup = BeautifulSoup(requests.get(url).content)
idx_table = soup.find('td', {'bgcolor': '#000000'}).parent.parent
trs = idx_table.findAll('tr')
indexes = {}
for tr in trs:
tds = tr.findAll('td', {'bgcolor': '#000000'})
if tds:
strip_re = re.compile(r'^\d+-')
city = strip_re.sub('', tds[0].getText()).title()
indexes[slugify(city)] = (city, tds[-1].getText())
return indexes
def rio_de_janeiro():
'''
Rio de Janeiro (all zones)
http://infoper.homeip.net:8800/smac/boletim.jsp
'''
url = 'http://infoper.homeip.net:8800/smac/boletim.jsp'
soup = BeautifulSoup(requests.get(url).content)
zones = soup.findAll('td', {'class': 'td_st_name'})
indexes = {}
for td in zones:
tr = td.parent
index = tr.findAll('td', {'class': 'td_value'})[-2].getText()
indexes[slugify(td.getText())] = (td.getText(), index)
return indexes
if __name__ == '__main__':
d_indexes = air_now()