From dfa5315dec03312eaa68748680968a703763643f Mon Sep 17 00:00:00 2001 From: Silas Brito Date: Wed, 26 Aug 2026 17:45:33 -0300 Subject: [PATCH] fix: extracao estrita do nome real da empresa ignorando cabecalho de Resultados do Google Maps --- backend/app/services/scraper_service.py | 40 ++++++++++++++++--------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/backend/app/services/scraper_service.py b/backend/app/services/scraper_service.py index f00f374..d847d32 100644 --- a/backend/app/services/scraper_service.py +++ b/backend/app/services/scraper_service.py @@ -104,18 +104,26 @@ class ScraperService: detail_panel = page.query_selector("div[role='main']") or page.query_selector("div.m6QEbd") detail_text = detail_panel.inner_text() if detail_panel else card.inner_text() - # 1. Nome da Empresa (h1 do painel ou aria-label do card) - h1_elem = page.query_selector("h1") - nome_empresa = "" - if h1_elem: - nome_empresa = h1_elem.inner_text().strip() - if not nome_empresa: - nome_empresa = (card.get_attribute("aria-label") or "").strip() - if not nome_empresa: - lines = [l.strip() for l in card.inner_text().split('\n') if l.strip()] - nome_empresa = lines[0] if lines else "" + # 1. Nome da Empresa (priorizar aria-label do card ou h1 dentro do detail_panel) + nome_empresa = (card.get_attribute("aria-label") or "").strip() + if nome_empresa.lower() in ('resultados', 'resultado', ''): + nome_empresa = "" + + if not nome_empresa and detail_panel: + h1_elem = detail_panel.query_selector("h1") + if h1_elem: + h_txt = h1_elem.inner_text().strip() + if h_txt and h_txt.lower() not in ('resultados', 'resultado'): + nome_empresa = h_txt if not nome_empresa: + lines = [l.strip() for l in card.inner_text().split('\n') if l.strip()] + for line in lines: + if line and line.lower() not in ('resultados', 'resultado') and not re.search(r'^[1-5][.,]\d', line): + nome_empresa = line + break + + if not nome_empresa or nome_empresa.lower() in ('resultados', 'resultado'): continue # 2. Telefone detalhado @@ -182,10 +190,14 @@ class ScraperService: else: # Se for resultado Ășnico direto try: - h1_elem = page.query_selector("h1") - if h1_elem: - nome_empresa = h1_elem.inner_text().strip() - detail_panel = page.query_selector("div[role='main']") or page.query_selector("div.m6QEbd") + detail_panel = page.query_selector("div[role='main']") or page.query_selector("div.m6QEbd") + h1_elem = detail_panel.query_selector("h1") if detail_panel else None + nome_empresa = h1_elem.inner_text().strip() if h1_elem else "" + if nome_empresa.lower() in ('resultados', 'resultado', ''): + title_elem = page.query_selector(".fontHeadlineLarge, .DUwif, div.qBF1Pd") + nome_empresa = title_elem.inner_text().strip() if title_elem else "" + + if nome_empresa and nome_empresa.lower() not in ('resultados', 'resultado'): detail_text = detail_panel.inner_text() if detail_panel else "" telefone_raw = ""