-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpeticiones.py
33 lines (29 loc) · 1.22 KB
/
peticiones.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
#!/usr/bin/env
#encoding=utf-8
from bs4 import BeautifulSoup
from tidylib import tidy_document # http://countergram.com/open-source/pytidylib/docs/index.html
import requests
def get_stations():
try:
web = requests.get('http://horarios.renfe.com/cer/hjcer300.jsp?NUCLEO=30&CP=NO&I=s#', timeout=4).text
except Timeout:
return None
else:
document, errors = tidy_document(web)
bs = BeautifulSoup(document, 'html.parser')
estaciones = bs.find('select', {"name":"o"}).findAll('option')
estaciones_ids = [(option.text.strip().replace(" ", "").lower(), option['value']) for option in estaciones][1:]
return {key: value for (key, value) in estaciones_ids}
def get_html(org, dst, date):
try:
r = requests.post('http://horarios.renfe.com/cer/hjcer310.jsp',
data = {'nucleo':'30', 'i':'s', 'cp':'NO',
'o':org, 'd':dst, 'df':date,
'ho':'00', 'hd':'26', 'TXTInfo':''}, timeout=4).text
except Timeout:
return None
else:
document, errors = tidy_document(r)
return document
if __name__ == "__main__":
print get_stations()