From 91b3d7aeed02fd2165d789b495ded4bc31d967e7 Mon Sep 17 00:00:00 2001 From: zik Date: Thu, 29 May 2025 20:56:04 +0300 Subject: [PATCH] =?UTF-8?q?=D0=B2=D0=B8=D0=B4=D0=B0=D0=BB=D0=B5=D0=BD?= =?UTF-8?q?=D0=BE=20=D0=BF=D1=80=D0=B5=D1=84=D1=96=D0=BA=D1=81=20"=D0=B7.?= =?UTF-8?q?=D0=BF.=20"=20=D0=B7=D1=96=20=D1=81=D1=82=D0=B0=D0=BD=D1=86?= =?UTF-8?q?=D1=96=D0=B9=20=D0=BF=D1=80=D0=B8=20=D0=BF=D0=B0=D1=80=D1=81?= =?UTF-8?q?=D0=B8=D0=BD=D0=B3=D1=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- parser.py | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/parser.py b/parser.py index fbc3dbe..4bfb274 100644 --- a/parser.py +++ b/parser.py @@ -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') - # Парсимо маршрути () для кожного потяга + # Маршрути для кожного поїзда 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):