-
Notifications
You must be signed in to change notification settings - Fork 4
/
tutiempo_crawler.py
370 lines (310 loc) · 16 KB
/
tutiempo_crawler.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
import requests
from bs4 import BeautifulSoup
import re
''' config_start '''
proxies = None
# proxies = {'http': 'http://127.0.0.1:1080',
# 'https': 'http://127.0.0.1:1080'}
''' config_end '''
def get_html(url):
''' request html
'''
r = requests.get(url=url,
proxies=proxies,
verify=False,
headers={'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'zh-CN,zh;q=0.9',
'Cache-Control': 'max-age=0',
'Connection': 'keep-alive',
'DNT': '1',
'Host': 'en.tutiempo.net',
'Referer': 'https://en.tutiempo.net/climate/1947/ws-545110.html',
'Upgrade-Insecure-Requests': '1',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36',
})
if r.status_code != 200:
print('warning: {} response with {}'.format(url, r.status_code))
return None
return str(r.content)
month_dic = {'january': '01',
'february': '02',
'march': '03',
'april': '04',
'may': '05',
'june': '06',
'july': '07',
'august': '08',
'september': '09',
'october': '10',
'november': '11',
'december': '12',
}
class tutiempo_location_obj(object):
str_location = ''
str_icao = ''
str_weather_station_id = ''
str_latitude = ''
str_longitude = ''
str_altitude = ''
class tutiempo_month_crawler(object):
location_obj = tutiempo_location_obj()
str_year = ''
str_month_en = ''
str_month_num = ''
array_climate_table_title = []
array_climate_table_subtitle = []
array_climate_table_data = []
@staticmethod
def get_obj(year, month, station_id):
if month < 1 or month > 12:
return None
url = 'https://en.tutiempo.net/climate/{month:02}-{year:04}/ws-{station_id}.html'.format(year=year, month=month, station_id=station_id)
html = get_html(url)
ret = tutiempo_month_crawler()
''' build symbol dict like:
# symbol_dict = {
# 'ntkk': '.',
# 'ntjj': '-',
# 'ntno': '1',
# 'ntvw': '2',
# 'ntef': '3',
# 'ntee': '4',
# 'ntgo': '5',
# 'ntdr': '6',
# 'ntpo': '7',
# 'nthy': '8',
# 'ntjg': '9',
# }
'''
symbol_dict = {}
str_symbol_style = re.compile(r'(?<=<style>).tablancpy([\s\S]*?)(?=</style>)').findall(html)
if str_symbol_style:
str_symbol_style = str(str_symbol_style)
symbols = re.compile(r'(?<=\sspan.)([\s\S]*?)(?=color)').findall(str(str_symbol_style))
for symbol in symbols:
key = str(re.compile(r'([\s\S]*?)(?=::after)').findall(str(symbol))[0])
val = str(re.compile(r'(?<=content:\S)([\s\S]*?)(?=\S;)').findall(str(symbol))[0])
symbol_dict[key.lower()] = val
bs = BeautifulSoup(html, 'html.parser')
''' get base info
'''
ret.location_obj.str_location = ''
ret.location_obj.str_icao = ''
ret.location_obj.str_weather_station_id = ''
ret.location_obj.str_latitude = ''
ret.location_obj.str_longitude = ''
ret.location_obj.str_altitude = ''
ret.str_year = ''
ret.str_month_en = ''
ret.str_month_num = ''
str_table_title = bs("title")[0].text
ret.location_obj.str_location = re.compile(r'(?<=Climate\s)[\s\S]+?(?=\s+?\(\w+?.*\s)').findall(str_table_title)[0]
ret.str_year = re.compile(r'Climate.*\s(\d+)\)\s-\s').findall(str_table_title)
if len(ret.str_year) >= 1 and str(ret.str_year[0]).isdigit():
ret.str_year = str(ret.str_year[0])
else:
ret.str_year = str(year)
ret.str_month_en = re.compile(r'Climate.+?[(](\w+)\s\d+\)').findall(str_table_title)
if len(ret.str_month_en) >= 1 and str(ret.str_month_en[0]).lower() in month_dic:
ret.str_month_en = str(ret.str_month_en[0]).lower()
ret.str_month_num = month_dic[ret.str_month_en]
else:
ret.str_month_en = list(month_dic.keys())[list(month_dic.values()).index(str(year))]
ret.str_month_num = str(month)
str_table_info = bs('p', attrs={'class': 'mt5'})
if not str_table_info:
str_table_info = ''
else:
str_table_info = str_table_info[0].text
ret.location_obj.str_weather_station_id = re.compile(r'(?<=station:\s)\d*(?=\s?\(?\S?\)?\s?)').findall(str_table_info)
if ret.location_obj.str_weather_station_id:
ret.location_obj.str_weather_station_id = ret.location_obj.str_weather_station_id[0]
tmp = re.compile(r'(?<={station}\s\()\S*(?=\))'.format(station = ret.location_obj.str_weather_station_id)).findall(str_table_info)
if tmp:
ret.location_obj.str_icao = tmp[0]
else:
ret.location_obj.str_icao = ''
else:
ret.location_obj.str_weather_station_id = ''
ret.location_obj.str_icao = ''
ret.location_obj.str_latitude = re.compile(r'(?<=Latitude):\s(-?\d+\.?\d*)').findall(str_table_info)
if ret.location_obj.str_latitude:
ret.location_obj.str_latitude = ret.location_obj.str_latitude[0]
else:
ret.location_obj.str_latitude = ''
ret.location_obj.str_longitude = re.compile(r'(?<=Longitude):\s(-?\d+\.?\d*)').findall(str_table_info)
if ret.location_obj.str_longitude:
ret.location_obj.str_longitude = ret.location_obj.str_longitude[0]
else:
ret.location_obj.str_longitude = ''
ret.location_obj.str_altitude = re.compile(r'(?<=Altitude):\s(-?\d+\.?\d*)').findall(str_table_info)
if ret.location_obj.str_altitude:
ret.location_obj.str_altitude = ret.location_obj.str_altitude[0]
else:
ret.location_obj.str_altitude = ''
''' get climate table data
'''
trs = bs.select('.mt5.minoverflow.tablancpy table tr') # climate table
# get table header
tr_header = trs[0]
# get trs of every day climate
trs_everyday = trs[1:-2]
# get table header
ret.array_climate_table_title = [th.text for th in tr_header.select('th')]
ret.array_climate_table_subtitle = ['', ] + [abbr['title'] for abbr in tr_header.select('abbr')]
# get table body (everyday climate)
ret.array_climate_table_data = []
for tr in trs_everyday:
# convert 'span - class' symbol to number
if tr.find_all('span'):
tds = tr.find_all('td')
array_line = []
array_line.append(tds[0].text)
for td in tds[1:]:
stritem = ''
for span in td.find_all('span'):
stritem = stritem + symbol_dict[span['class'][0].lower()]
array_line.append(stritem)
# save directly
else:
array_line = [td.text.replace('\xa0', '') for td in tr]
# make 1st clomn into datetime
array_line[0] = '{year}-{month}-{day:02d}'.format(year=ret.str_year, month=ret.str_month_num, day=int(array_line[0]))
ret.array_climate_table_data.append(array_line)
return ret
class tutiempo_location_crawler(object):
@staticmethod
def __get_locations_by_region(name):
locations = []
i = 1
while True:
html = get_html('https://en.tutiempo.net/climate/{0}/{1}/'.format(name, i))
if html == None or html.find('Error 404') >= 0:
break
bs = BeautifulSoup(html, 'html.parser')
location_links = bs.select('.mlistados ul li a')
for l in location_links:
obj = tutiempo_location_obj()
obj.str_location = l.text
obj.str_weather_station_id = l['href'].replace('/climate/ws-', '').replace('.html', '')
locations.append(obj)
i = i + 1
return locations
@staticmethod
def __get_location_details(locations):
for location_obj in locations:
html = get_html('https://en.tutiempo.net/climate/ws-{0}.html'.format(location_obj.str_weather_station_id))
if html == None or html.find('Error 404') >= 0:
continue
bs = BeautifulSoup(html, 'html.parser')
str_table_info = bs('p', attrs={'class': 'mt5'})
if not str_table_info:
str_table_info = ''
else:
str_table_info = str_table_info[0].text
location_obj.str_weather_station_id = re.compile(r'(?<=station:\s)\d*(?=\s?\(?\S?\)?\s?)').findall(str_table_info)
if location_obj.str_weather_station_id:
location_obj.str_weather_station_id = location_obj.str_weather_station_id[0]
tmp = re.compile(r'(?<={station}\s\()\S*(?=\))'.format(station = location_obj.str_weather_station_id)).findall(str_table_info)
if tmp:
location_obj.str_icao = tmp[0]
else:
location_obj.str_icao = ''
else:
location_obj.str_weather_station_id = ''
location_obj.str_icao = ''
location_obj.str_latitude = re.compile(r'(?<=Latitude):\s(\d+\.?\d*)').findall(str_table_info)
if location_obj.str_latitude:
location_obj.str_latitude = location_obj.str_latitude[0]
else:
location_obj.str_latitude = ''
location_obj.str_longitude = re.compile(r'(?<=Longitude):\s(\d+\.?\d*)').findall(str_table_info)
if location_obj.str_longitude:
location_obj.str_longitude = location_obj.str_longitude[0]
else:
location_obj.str_longitude = ''
location_obj.str_altitude = re.compile(r'(?<=Altitude):\s(\d+\.?\d*)').findall(str_table_info)
if location_obj.str_altitude:
location_obj.str_altitude = location_obj.str_altitude[0]
else:
location_obj.str_altitude = ''
pass
@staticmethod
def get_locations_by_region(name):
'''
return array of tutiempo_location_obj
'''
locations = tutiempo_location_crawler.__get_locations_by_region(name)
tutiempo_location_crawler.__get_location_details(locations)
return locations
class tutiempo_climate_data(object):
location_obj = tutiempo_location_obj()
array_climate_table_title = []
array_climate_table_subtitle = []
array_climate_table_data = []
def append(self, month_data, int_year, int_month):
# base info
if len(self.array_climate_table_data) == 0:
self.location_obj.str_location = month_data.location_obj.str_location
self.location_obj.str_icao = month_data.location_obj.str_icao
self.location_obj.str_weather_station_id = month_data.location_obj.str_weather_station_id
self.location_obj.str_latitude = month_data.location_obj.str_latitude
self.location_obj.str_longitude = month_data.location_obj.str_longitude
self.location_obj.str_altitude = month_data.location_obj.str_altitude
self.array_climate_table_title = month_data.array_climate_table_title[:]
self.array_climate_table_subtitle = month_data.array_climate_table_subtitle[:]
self.array_climate_table_data = month_data.array_climate_table_data[:]
elif self.location_obj.str_location == month_data.location_obj.str_location:
self.array_climate_table_data = self.array_climate_table_data + month_data.array_climate_table_data[:]
pass
else :
print("warning: try to append {0} into {1}\n".format(month_data.location_obj.str_location,self.location_obj.str_location))
return self
'''
simple demo
'''
if __name__ == '__main__':
region_name = 'macau'
# get locations of
locations = tutiempo_location_crawler.get_locations_by_region(region_name)
if locations and len(locations) > 0:
# build csv
array_title = ['location', 'weather station','ICAO','Latitude','Longitude','Altitude']
array_data = []
for loc in locations:
array_data.append([loc.str_location, loc.str_weather_station_id, loc.str_icao, loc.str_latitude, loc.str_longitude, loc.str_altitude])
write_data = '"\n"'.join(['","'.join(tr)for tr in [array_title,] + array_data])
write_data = '"' + write_data + '"'
# write csv
with open('{region_name}_locations.csv'.format(region_name=region_name), 'w') as fp:
fp.write(write_data)
year = 2018
station_id = '744860'
# get one month
month_data = tutiempo_month_crawler.get_obj(year, 1, station_id)
if month_data:
# build csv
array_title = ['location: ', month_data.location_obj.str_location, 'Year: ', month_data.str_year, 'Month: ', month_data.str_month_num]
array_info = ['Weather station: ', month_data.location_obj.str_weather_station_id, 'ICAO', month_data.location_obj.str_icao, 'Latitude: ', month_data.location_obj.str_latitude, 'Longitude: ', month_data.location_obj.str_longitude, 'Altitude: ', month_data.location_obj.str_altitude]
write_data = '"\n"'.join(['","'.join(tr)for tr in [array_title, array_info, month_data.array_climate_table_title, ] + [month_data.array_climate_table_subtitle, ] + month_data.array_climate_table_data])
write_data = '"' + write_data + '"'
# write csv
with open('S{id}_{year}-{month}_climate.csv'.format(id=month_data.location_obj.str_weather_station_id, year=month_data.str_year, month=month_data.str_month_num), 'w') as fp:
fp.write(write_data)
# get one year
year_data = tutiempo_climate_data()
for i in range(1,13):
month_data = tutiempo_month_crawler.get_obj(year, i, station_id)
if month_data:
year_data.append(month_data,year,i)
# build csv
array_title = ['location: ', year_data.location_obj.str_location]
array_info = ['Weather station: ', year_data.location_obj.str_weather_station_id, 'ICAO', year_data.location_obj.str_icao, 'Latitude: ', year_data.location_obj.str_latitude, 'Longitude: ', year_data.location_obj.str_longitude, 'Altitude: ', year_data.location_obj.str_altitude]
write_data = '"\n"'.join(['","'.join(tr)for tr in [array_title, array_info, year_data.array_climate_table_title, ] + [year_data.array_climate_table_subtitle, ] + year_data.array_climate_table_data])
write_data = '"' + write_data + '"'
# write csv
with open('S{id}_{year}_climate.csv'.format(id=year_data.location_obj.str_weather_station_id, year=year), 'w') as fp:
fp.write(write_data)
pass