domovoy_bot/scraper_gis/get_data_v2.py

58 lines
2.4 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
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]
print(f'Attached. URL: {page.url}')
# Скроллим вниз, чтобы подгрузились элементы
page.evaluate('window.scrollTo(0, document.body.scrollHeight)')
time.sleep(3)
data = []
# Ищем все строки таблицы
rows = page.query_selector_all('tr')
print(f'Found {len(rows)} potential rows.')
for row in rows:
inner_text = row.inner_text()
if '73-20' in inner_text:
# Нашли строку с обращением
link = row.query_selector('a[href*="appeals/view/"]')
if link:
num = link.inner_text().strip()
href = link.get_attribute('href')
# Достаем статус
status = 'Unknown'
if 'Исполнено' in inner_text: status = 'Исполнено'
data.append({
'number': num,
'href': href,
'status': status
})
with open('/app/found_appeals.json', 'w', encoding='utf-8') as f:
json.dump(data, f, ensure_ascii=False, indent=2)
print(f'CAPTURED: {len(data)} appeals.')
# Если нашли хотя бы одно, попробуем зайти
if data:
target = data[0]
print(f'Attempting to enter: {target["number"]}')
page.goto('https://my.dom.gosuslugi.ru' + target['href'])
page.wait_for_load_state('networkidle')
time.sleep(10)
page.screenshot(path='/app/appeal_detail_view.png', full_page=True)
print(f'Detail screenshot saved for {target["number"]}')
browser.close()
except Exception as e:
print(f'ERROR: {e}')
if __name__ == '__main__':
run()