domovoy_bot/scraper_gis/get_full_queue.py

58 lines
2.5 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from playwright.sync_api import sync_playwright
import json, time, re
def run():
with sync_playwright() as p:
try:
browser = p.chromium.connect_over_cdp('http://127.0.0.1:9222')
page = browser.contexts[0].pages[0]
# 1. Идем в список
page.goto('https://my.dom.gosuslugi.ru/citizen-cabinet/#!/appeals/applicant/search')
page.wait_for_load_state('networkidle')
time.sleep(10)
# 2. Пытаемся выставить 'По 100'
try:
# Ищем селект пагинации
page.locator('select.page-size-selector, .ui-select-container').click()
time.sleep(2)
page.get_by_text('100').click()
time.sleep(10)
except:
print('Не удалось выставить "по 100", будем собирать что есть.')
full_queue = []
# 3. Собираем со всех страниц (если их несколько)
while True:
rows = page.locator('tr').all()
for row in rows:
text = row.inner_text()
if '73-20' in text:
link = row.locator('a[href*="appeals/view/"]').first
num_match = re.search(r'73-202\d-\d+', text)
num = num_match.group(0) if num_match else "unknown"
href = link.get_attribute('href')
if href and num not in [x['number'] for x in full_queue]:
full_queue.append({'number': num, 'href': href})
# Ищем кнопку Следующая
next_btn = page.locator('a:has-text("следующая")')
if next_btn.is_visible() and next_btn.is_enabled():
print(f'Переход на следующую страницу... Уже собрано: {len(full_queue)}')
next_btn.click()
time.sleep(10)
else:
break
with open('/app/services/gis_harvester/data/full_queue.json', 'w', encoding='utf-8') as f:
json.dump(full_queue, f, ensure_ascii=False, indent=2)
print(f'ИТОГО СОБРАНО: {len(full_queue)} целей.')
browser.close()
except Exception as e:
print(f'ERROR: {e}')
if __name__ == "__main__":
run()