видалено префікс "з.п. " зі станцій при парсингу
This commit is contained in:
parent
73fa27209f
commit
91b3d7aeed
24
parser.py
24
parser.py
@ -35,6 +35,7 @@ def parse_days(text: str) -> str:
|
|||||||
return ''.join(str(b) for b in mask)
|
return ''.join(str(b) for b in mask)
|
||||||
return '1111111'
|
return '1111111'
|
||||||
|
|
||||||
|
|
||||||
def fetch_schedule(tab: int = 1, use_local: bool = False) -> List[Dict]:
|
def fetch_schedule(tab: int = 1, use_local: bool = False) -> List[Dict]:
|
||||||
"""
|
"""
|
||||||
Парсить вкладку розкладу:
|
Парсить вкладку розкладу:
|
||||||
@ -60,36 +61,39 @@ def fetch_schedule(tab: int = 1, use_local: bool = False) -> List[Dict]:
|
|||||||
if not times_table:
|
if not times_table:
|
||||||
raise RuntimeError(f'Не знайдено таблицю розкладу для tab={tab}')
|
raise RuntimeError(f'Не знайдено таблицю розкладу для tab={tab}')
|
||||||
|
|
||||||
# Список станцій (35)
|
# Список станцій (35), очищаємо префікс 'з.п. '
|
||||||
station_tags = soup.select(
|
station_tags = soup.select(
|
||||||
f'{prefix} table.left tr.on a.et, '
|
f'{prefix} table.left tr.on a.et, '
|
||||||
f'{prefix} table.left tr.onx a.et'
|
f'{prefix} table.left tr.onx a.et'
|
||||||
)
|
)
|
||||||
stations = [a.get_text(strip=True) for a in station_tags]
|
stations = []
|
||||||
|
for a in station_tags:
|
||||||
|
raw = a.get_text(strip=True)
|
||||||
|
# Видаляємо 'з.п. ' на початку
|
||||||
|
clean = raw
|
||||||
|
if clean.startswith('з.п. '):
|
||||||
|
clean = clean[len('з.п. '):]
|
||||||
|
stations.append(clean)
|
||||||
|
|
||||||
# Ряди таблиці
|
# Рядки таблиці
|
||||||
trs = times_table.find_all('tr')
|
trs = times_table.find_all('tr')
|
||||||
|
|
||||||
# Рядок з номерами потягів та днями курсування
|
|
||||||
header_row = next(r for r in trs if r.find('td', class_='on_right_t'))
|
header_row = next(r for r in trs if r.find('td', class_='on_right_t'))
|
||||||
train_cells = header_row.find_all('td', class_='on_right_t')
|
train_cells = header_row.find_all('td', class_='on_right_t')
|
||||||
|
|
||||||
# Парсимо маршрути (<td class="course">) для кожного потяга
|
# Маршрути для кожного поїзда
|
||||||
route_tags = times_table.select('td.course')
|
route_tags = times_table.select('td.course')
|
||||||
routes = [tag.get_text(strip=True) for tag in route_tags[:len(train_cells)]]
|
routes = [tag.get_text(strip=True) for tag in route_tags[:len(train_cells)]]
|
||||||
|
|
||||||
entries: List[Dict] = []
|
entries: List[Dict] = []
|
||||||
for idx, cell in enumerate(train_cells):
|
for idx, cell in enumerate(train_cells):
|
||||||
# Витягнути унікальний tid з href
|
|
||||||
a_tag = cell.find('a', class_='et')
|
a_tag = cell.find('a', class_='et')
|
||||||
href = a_tag['href'] # наприклад ".?tid=26397"
|
href = a_tag['href']
|
||||||
tid = href.split('tid=')[-1]
|
tid = href.split('tid=')[-1]
|
||||||
|
|
||||||
parts = cell.get_text(separator='|', strip=True).split('|')
|
parts = cell.get_text(separator='|', strip=True).split('|')
|
||||||
num = parts[0].rstrip(',').strip()
|
num = parts[0].rstrip(',').strip()
|
||||||
days = parse_days(parts[1] if len(parts) > 1 else 'щоденно')
|
days = parse_days(parts[1] if len(parts) > 1 else 'щоденно')
|
||||||
route = routes[idx] if idx < len(routes) else ''
|
route = routes[idx] if idx < len(routes) else ''
|
||||||
|
|
||||||
entries.append({
|
entries.append({
|
||||||
'tid': tid,
|
'tid': tid,
|
||||||
'train_number': num,
|
'train_number': num,
|
||||||
@ -100,8 +104,6 @@ def fetch_schedule(tab: int = 1, use_local: bool = False) -> List[Dict]:
|
|||||||
|
|
||||||
# Рядки з часами руху (повинно бути 35)
|
# Рядки з часами руху (повинно бути 35)
|
||||||
time_rows = [r for r in trs if r.find('td', class_='q0') or r.find('td', class_='q1')]
|
time_rows = [r for r in trs if r.find('td', class_='q0') or r.find('td', class_='q1')]
|
||||||
|
|
||||||
# Збирання часу для кожного поїзда та станції
|
|
||||||
for idx, entry in enumerate(entries):
|
for idx, entry in enumerate(entries):
|
||||||
base = idx * 3
|
base = idx * 3
|
||||||
for si, row in enumerate(time_rows):
|
for si, row in enumerate(time_rows):
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user