видалено префікс "з.п. " зі станцій при парсингу

This commit is contained in:
Max Lakhman 2025-05-29 20:56:04 +03:00
parent 73fa27209f
commit 91b3d7aeed

View File

@ -35,6 +35,7 @@ def parse_days(text: str) -> str:
return ''.join(str(b) for b in mask)
return '1111111'
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:
raise RuntimeError(f'Не знайдено таблицю розкладу для tab={tab}')
# Список станцій (35)
# Список станцій (35), очищаємо префікс 'з.п. '
station_tags = soup.select(
f'{prefix} table.left tr.on 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')
# Рядок з номерами потягів та днями курсування
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')
# Парсимо маршрути (<td class="course">) для кожного потяга
# Маршрути для кожного поїзда
route_tags = times_table.select('td.course')
routes = [tag.get_text(strip=True) for tag in route_tags[:len(train_cells)]]
entries: List[Dict] = []
for idx, cell in enumerate(train_cells):
# Витягнути унікальний tid з href
a_tag = cell.find('a', class_='et')
href = a_tag['href'] # наприклад ".?tid=26397"
href = a_tag['href']
tid = href.split('tid=')[-1]
parts = cell.get_text(separator='|', strip=True).split('|')
num = parts[0].rstrip(',').strip()
days = parse_days(parts[1] if len(parts) > 1 else 'щоденно')
route = routes[idx] if idx < len(routes) else ''
entries.append({
'tid': tid,
'train_number': num,
@ -100,8 +104,6 @@ def fetch_schedule(tab: int = 1, use_local: bool = False) -> List[Dict]:
# Рядки з часами руху (повинно бути 35)
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):
base = idx * 3
for si, row in enumerate(time_rows):