domovoy_bot/scraper_gis/get_full_queue_deep.py

73 lines
3.2 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]
page.goto('https://my.dom.gosuslugi.ru/citizen-cabinet/#!/appeals/applicant/search')
page.wait_for_load_state('networkidle')
time.sleep(10)
# Нажимаем 'Найти' для сброса фильтров
print('Нажимаем Найти...')
page.locator('button:has-text("Найти")').click()
time.sleep(10)
# Пытаемся выставить 'По 100'
try:
page.evaluate('''() => {
const el = Array.from(document.querySelectorAll('span, div')).find(e => e.innerText === '10');
if (el) el.click();
}''')
time.sleep(2)
page.locator('div.ui-select-choices-row:has-text("100")').click()
print('Выбрано по 100.')
time.sleep(10)
except: pass
full_queue = []
page_num = 1
while True:
# Очистка оверлеев
page.evaluate('document.querySelectorAll(".p-dialog-mask, .modal-backdrop").forEach(el => el.remove())')
rows = page.locator('tr').all()
added_on_page = 0
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 f"unknown_{time.time()}"
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})
added_on_page += 1
print(f'Страница {page_num}: собрано {added_on_page} новых.')
# Ищем стрелку вправо или слово 'следующая'
next_btn = page.locator('a:has-text("следующая"), .pagination-next a').first
if next_btn.is_visible() and next_btn.is_enabled():
page.evaluate('el => el.click()', next_btn.element_handle())
page.wait_for_load_state('networkidle')
time.sleep(10)
page_num += 1
else:
break
output_path = '/app/services/gis_harvester/data/full_queue.json'
with open(output_path, '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()