-
Notifications
You must be signed in to change notification settings - Fork 0
/
chebro_get_stations.py
executable file
·70 lines (62 loc) · 2.61 KB
/
chebro_get_stations.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
#!/usr/bin/env python
import requests
import re
import json
import utm
pat = re.compile(r"<option value='([^']*)'>([^<]*)</option>")
def getHtml():
r = requests.get('http://195.55.247.237/saihebro/index.php?url=/datos/mapas/tipoestacion:A')
return r.text.encode(r.encoding)
def getOptions(html):
idx = html.find("<option value='' selected='selected'>(Localizar estación)</option>")
idx2 = html.find("</select>", idx)
return html[idx:idx2]
def getStationDataHtml(station_id):
r = requests.get('http://195.55.247.237/saihebro/index.php?url=/datos/ficha/estacion:%s'%station_id)
return r.text.encode(r.encoding)
def parseStation(html):
idx = html.find('<a href="javascript:cambiarCapa(1,4,\'ficha\');" >')
idx2 = html.find('</a>', idx)
desc = html[idx+len('<a href="javascript:cambiarCapa(1,4,\'ficha\');" >'):idx2]
print(desc)
idx = html.find('<td class="celdacn">Z</td>',idx2)
idx = html.find('<td class="celdac" colspan="2">',idx)
idx2 = html.find('</td>',idx)
h = int(html[idx+len('<td class="celdac" colspan="2">'):idx2])
idx = html.find('<td class="celdac">',idx2)
idx2 = html.find('</td>',idx)
x = float(html[idx+len('<td class="celdac">'):idx2].replace(',','.'))
idx = html.find('<td class="celdac">',idx2)
idx2 = html.find('</td>',idx)
y = float(html[idx+len('<td class="celdac">'):idx2].replace(',','.'))
idx = html.find('<td class="celdac">',idx2)
idx2 = html.find('</td>',idx)
z = float(html[idx+len('<td class="celdac">'):idx2].replace(',','.'))
idx = html.find('<td class="celdaln" colspan="5">Río:</td>')
idx = html.find('<td class="celdal" colspan="5">',idx)
idx2 = html.find('</td>',idx)
river = html[idx+len('<td class="celdal" colspan="5">'):idx2]
print(river)
idx = html.find('<td class="celdal">CAUDAL',idx2)
idx = html.find('index.php?url=/datos/graficas/tag:', idx)
idx2 = html.find('"', idx)
tag = html[idx+len('index.php?url=/datos/graficas/tag:'):idx2]
if (len(tag)>100):
tag = ''
print(tag)
return desc,h,x,y,z,river,tag
def main():
html = getHtml()
options = getOptions(html)
values = pat.findall(options)
stations = []
for v,t in values:
station_id,zone,station_type = v.split('|')
print(station_id)
name = ' '.join(t.replace(' ',' ').split()[1:])
desc,h,x,y,z,river,tag = parseStation(getStationDataHtml(station_id))
lat,lon = utm.to_latlon(x,y,h,'T')
stations.append({'id':station_id,'tag':tag,'zone':zone,'type':station_type,'name':name,'desc':desc,'utm_x':x,'utm_y':y,'alt':z,'river':river,'lat':lat,'lon':lon})
json.dump(stations,open('stations_chebro.json','w'))
if __name__=='__main__':
main()