File size: 2,766 Bytes
358f05c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
from fastapi import Query

@app.get("/search_leads")
async def search_leads(
    query: str = Query(..., description="Search term for business leads")
):
    logger.info(f"Searching Google Maps for: {query}")

    async with async_playwright() as p:
        browser = await p.chromium.launch(headless=True)
        page = await browser.new_page()

        try:
            # Go to Google Maps
            await page.goto("https://www.google.com/maps", wait_until="networkidle")

            # Accept cookies if present (optional, depends on region)
            try:
                await page.click('button[aria-label="Accept all"]', timeout=3000)
            except:
                pass

            # Type the query in the search box and press Enter
            await page.fill('input#searchboxinput', query)
            await page.click('button#searchbox-searchbutton')

            # Wait for search results to load - selector for listings container
            await page.wait_for_selector('div[role="article"]', timeout=10000)

            # Scroll results container to load more items (optional)
            # For now, scrape the visible ones

            # Extract data from listings
            results = await page.evaluate("""
                () => {
                    const listings = [];
                    const elements = document.querySelectorAll('div[role="article"]');
                    elements.forEach(el => {
                        const nameEl = el.querySelector('h3 span');
                        const name = nameEl ? nameEl.innerText : null;

                        const addressEl = el.querySelector('[data-tooltip="Address"]');
                        const address = addressEl ? addressEl.innerText : null;

                        const phoneEl = el.querySelector('button[data-tooltip="Copy phone number"]');
                        const phone = phoneEl ? phoneEl.getAttribute('aria-label')?.replace('Copy phone number ', '') : null;

                        const websiteEl = el.querySelector('a[aria-label*="Website"]');
                        const website = websiteEl ? websiteEl.href : null;

                        listings.push({name, address, phone, website});
                    });
                    return listings;
                }
            """)

            await browser.close()

            # Filter out empty entries
            filtered = [r for r in results if r['name']]

            return {"query": query, "results_count": len(filtered), "results": filtered}

        except Exception as e:
            await browser.close()
            logger.error(f"Error during Google Maps search scraping: {str(e)}")
            raise HTTPException(status_code=500, detail=f"Search scraping error: {str(e)}")