Commit
·
358f05c
1
Parent(s):
f9275f7
Added test1
Browse files- Dockerfile +1 -1
- __pycache__/scrape.cpython-39.pyc +0 -0
- test1.py +67 -0
Dockerfile
CHANGED
@@ -53,4 +53,4 @@ RUN python -m playwright install chromium
|
|
53 |
EXPOSE 7860
|
54 |
|
55 |
# Run the FastAPI application
|
56 |
-
CMD ["python", "-m", "uvicorn", "
|
|
|
53 |
EXPOSE 7860
|
54 |
|
55 |
# Run the FastAPI application
|
56 |
+
CMD ["python", "-m", "uvicorn", "test1:app", "--host", "0.0.0.0", "--port", "7860"]
|
__pycache__/scrape.cpython-39.pyc
ADDED
Binary file (2.44 kB). View file
|
|
test1.py
ADDED
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import Query
|
2 |
+
|
3 |
+
@app.get("/search_leads")
|
4 |
+
async def search_leads(
|
5 |
+
query: str = Query(..., description="Search term for business leads")
|
6 |
+
):
|
7 |
+
logger.info(f"Searching Google Maps for: {query}")
|
8 |
+
|
9 |
+
async with async_playwright() as p:
|
10 |
+
browser = await p.chromium.launch(headless=True)
|
11 |
+
page = await browser.new_page()
|
12 |
+
|
13 |
+
try:
|
14 |
+
# Go to Google Maps
|
15 |
+
await page.goto("https://www.google.com/maps", wait_until="networkidle")
|
16 |
+
|
17 |
+
# Accept cookies if present (optional, depends on region)
|
18 |
+
try:
|
19 |
+
await page.click('button[aria-label="Accept all"]', timeout=3000)
|
20 |
+
except:
|
21 |
+
pass
|
22 |
+
|
23 |
+
# Type the query in the search box and press Enter
|
24 |
+
await page.fill('input#searchboxinput', query)
|
25 |
+
await page.click('button#searchbox-searchbutton')
|
26 |
+
|
27 |
+
# Wait for search results to load - selector for listings container
|
28 |
+
await page.wait_for_selector('div[role="article"]', timeout=10000)
|
29 |
+
|
30 |
+
# Scroll results container to load more items (optional)
|
31 |
+
# For now, scrape the visible ones
|
32 |
+
|
33 |
+
# Extract data from listings
|
34 |
+
results = await page.evaluate("""
|
35 |
+
() => {
|
36 |
+
const listings = [];
|
37 |
+
const elements = document.querySelectorAll('div[role="article"]');
|
38 |
+
elements.forEach(el => {
|
39 |
+
const nameEl = el.querySelector('h3 span');
|
40 |
+
const name = nameEl ? nameEl.innerText : null;
|
41 |
+
|
42 |
+
const addressEl = el.querySelector('[data-tooltip="Address"]');
|
43 |
+
const address = addressEl ? addressEl.innerText : null;
|
44 |
+
|
45 |
+
const phoneEl = el.querySelector('button[data-tooltip="Copy phone number"]');
|
46 |
+
const phone = phoneEl ? phoneEl.getAttribute('aria-label')?.replace('Copy phone number ', '') : null;
|
47 |
+
|
48 |
+
const websiteEl = el.querySelector('a[aria-label*="Website"]');
|
49 |
+
const website = websiteEl ? websiteEl.href : null;
|
50 |
+
|
51 |
+
listings.push({name, address, phone, website});
|
52 |
+
});
|
53 |
+
return listings;
|
54 |
+
}
|
55 |
+
""")
|
56 |
+
|
57 |
+
await browser.close()
|
58 |
+
|
59 |
+
# Filter out empty entries
|
60 |
+
filtered = [r for r in results if r['name']]
|
61 |
+
|
62 |
+
return {"query": query, "results_count": len(filtered), "results": filtered}
|
63 |
+
|
64 |
+
except Exception as e:
|
65 |
+
await browser.close()
|
66 |
+
logger.error(f"Error during Google Maps search scraping: {str(e)}")
|
67 |
+
raise HTTPException(status_code=500, detail=f"Search scraping error: {str(e)}")
|