-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscraper.py
More file actions
400 lines (346 loc) · 14.9 KB
/
Copy pathscraper.py
File metadata and controls
400 lines (346 loc) · 14.9 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
import asyncio
import os
import re
import sys
from datetime import datetime, timedelta
from typing import Dict, Optional, Set, List
from crawlee.crawlers import PlaywrightCrawler
from crawlee import Request
from playwright.async_api import Page, ElementHandle
import pandas as pd
class GoogleMapsScraper:
"""
Scraper for extracting business listing data from Google Maps.
Phase 1: Scrolls search results and enqueues every business detail page.
Phase 2: Visits each detail page to extract full address, phone, and more.
Appends each run's data to a new sheet in the Excel file.
"""
def __init__(self, headless: bool = True, timeout_minutes: int = 10):
self.crawler = PlaywrightCrawler(
headless=headless,
request_handler_timeout=timedelta(minutes=timeout_minutes),
)
self.results: List[Dict] = []
self.processed_urls: Set[str] = set()
self.search_query: str = ""
self.detail_count: int = 0
async def setup_crawler(self) -> None:
"""Configure the crawler with a single default handler that routes by URL."""
self.crawler.router.default_handler(self._handle_request)
async def _handle_request(self, context) -> None:
"""Route each request to the correct handler based on URL pattern."""
url = context.request.url
if "/place/" in url:
await self._handle_detail_page(context)
else:
await self._handle_search_results(context)
async def _handle_search_results(self, context) -> None:
"""Scroll the search results feed and enqueue every business detail page."""
page = context.page
context.log.info(f"\n{'='*50}")
context.log.info(f"SEARCH PHASE: {context.request.url}")
context.log.info(f"{'='*50}\n")
await page.wait_for_selector(".Nv2PK", timeout=30000)
await page.wait_for_timeout(2000)
enqueued = 0
while True:
listings = await page.query_selector_all(".Nv2PK")
batch_new = 0
for listing in listings:
link_el = await listing.query_selector("a.hfpxzc")
if not link_el:
continue
href = await link_el.get_attribute("href")
if not href or href in self.processed_urls:
continue
self.processed_urls.add(href)
# Grab the name from the search card so we can pass it to the detail handler
name_el = await listing.query_selector(".qBF1Pd")
name = await name_el.inner_text() if name_el else None
# Enqueue the detail page using proper Request object
try:
req = Request.from_url(
href,
user_data={"name_from_search": name}
)
await context.add_requests([req])
enqueued += 1
batch_new += 1
context.log.info(f"Enqueued detail page: {name}")
except Exception as e:
context.log.warning(f"Failed to enqueue {href}: {e}")
# Scroll feed to load more results
has_more = await self._load_more_items(page)
if batch_new == 0 and not has_more:
break
context.log.info(f"\nSearch phase complete. {enqueued} detail pages queued.")
async def _handle_detail_page(self, context) -> None:
"""Visit a single business page and extract full details."""
page = context.page
url = context.request.url
# Retrieve name passed from search results (if available)
name = None
user_data = getattr(context.request, "user_data", None) or {}
if isinstance(user_data, dict):
name = user_data.get("name_from_search")
# Wait for detail page to render
await page.wait_for_timeout(3000)
try:
# --- Name (fallback if not passed from search) ---
if not name:
for sel in ("h1", '[role="main"] h1', ".lMbq3e"):
el = await page.query_selector(sel)
if el:
text = await el.inner_text()
if text:
name = text.strip()
break
# --- Address ---
address = None
address_selectors = [
'button[data-item-id="address"]',
'[data-item-id="address"]',
'button[aria-label*="Address"]',
'[data-tooltip="Copy address"]',
'div.rogA2c',
]
for sel in address_selectors:
el = await page.query_selector(sel)
if el:
text = await el.inner_text()
if text and len(text) > 5:
address = text.strip()
break
# --- Phone Number ---
phone = None
phone_selectors = [
'button[data-item-id^="phone"]',
'[data-item-id^="phone"]',
'button[aria-label*="Phone"]',
'a[href^="tel:"]',
]
for sel in phone_selectors:
el = await page.query_selector(sel)
if el:
text = await el.inner_text()
if text and any(c.isdigit() for c in text):
phone = text.strip()
break
aria = await el.get_attribute("aria-label")
if aria and any(c.isdigit() for c in aria):
phone = aria.strip()
break
# --- Rating ---
rating = None
rating_selectors = [
'div.F7nice span:first-child',
'span[aria-label*="star"]',
'div[role="main"] span[aria-label*="stars"]',
]
for sel in rating_selectors:
el = await page.query_selector(sel)
if el:
text = await el.inner_text()
if text and any(c.isdigit() for c in text):
rating = text.strip()
break
# --- Reviews Count ---
reviews = None
reviews_selectors = [
'div.F7nice span + span',
'button[aria-label*="review"]',
'span[aria-label*="reviews"]',
]
for sel in reviews_selectors:
el = await page.query_selector(sel)
if el:
text = await el.inner_text()
if text and ("review" in text.lower() or "(" in text):
reviews = text.strip().strip("()")
break
# --- Website ---
website = None
website_selectors = [
'button[data-item-id="authority"]',
'a[data-item-id="authority"]',
'a[aria-label*="Website"]',
]
for sel in website_selectors:
el = await page.query_selector(sel)
if el:
href = await el.get_attribute("href")
if href and "google.com" not in href:
website = href
break
# --- Opening Hours ---
hours = None
hours_el = await page.query_selector('button[data-item-id="oh"]')
if hours_el:
hours = await hours_el.inner_text()
# --- Category ---
category = None
category_selectors = [
'button[jsaction*="category"]',
'span[aria-label*="Category"]',
'div[role="main"] div span',
]
for sel in category_selectors:
el = await page.query_selector(sel)
if el:
text = await el.inner_text()
if text and text != name and len(text) < 60:
category = text.strip()
break
# --- Price Level ---
price = None
price_el = await page.query_selector('span[aria-label*="Price"]')
if price_el:
price = await price_el.inner_text()
place_data = {
"name": name,
"category": category,
"rating": rating,
"reviews": reviews,
"price": price,
"address": address,
"phone": phone,
"website": website,
"hours": hours,
"url": url,
}
self.results.append(place_data)
self.detail_count += 1
context.log.info(f"Extracted: {name} | {address} | {phone}")
except Exception as e:
context.log.exception(f"Error on detail page {url}")
async def _load_more_items(self, page: Page) -> bool:
"""Scroll the search results feed to trigger lazy loading."""
try:
feed = await page.query_selector('div[role="feed"]')
if not feed:
return False
prev_scroll = await feed.evaluate("(element) => element.scrollTop")
await feed.evaluate("(element) => element.scrollTop += 800")
await page.wait_for_timeout(2000)
new_scroll = await feed.evaluate("(element) => element.scrollTop")
if new_scroll <= prev_scroll:
return False
await page.wait_for_timeout(1000)
return True
except Exception:
return False
def _sanitize_sheet_name(self, name: str) -> str:
"""Ensure Excel-compatible sheet name (max 31 chars, no forbidden chars)."""
name = re.sub(r'[\\/*?:[\]]', '_', name)
return name[:31]
def export_to_excel(self, filename: str = "gmap_data.xlsx", sheet_name: Optional[str] = None) -> None:
"""Export scraped data to a new sheet in the Excel file."""
if not self.results:
print("No data to export.")
return
df = pd.DataFrame(self.results)
column_order = [
"name", "category", "rating", "reviews", "price",
"address", "phone", "website", "hours", "url"
]
available_columns = [c for c in column_order if c in df.columns]
for c in df.columns:
if c not in available_columns:
available_columns.append(c)
df = df[available_columns]
if sheet_name is None:
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
query_slug = self.search_query.replace(" ", "_")[:20] if self.search_query else "data"
sheet_name = f"{query_slug}_{timestamp}"
sheet_name = self._sanitize_sheet_name(sheet_name)
file_exists = os.path.exists(filename)
try:
if file_exists:
with pd.ExcelWriter(
filename,
engine='openpyxl',
mode='a',
if_sheet_exists='new'
) as writer:
df.to_excel(writer, index=False, sheet_name=sheet_name)
worksheet = writer.sheets[sheet_name]
self._adjust_column_widths(worksheet)
else:
with pd.ExcelWriter(filename, engine='openpyxl') as writer:
df.to_excel(writer, index=False, sheet_name=sheet_name)
worksheet = writer.sheets[sheet_name]
self._adjust_column_widths(worksheet)
print(f"\n{'='*50}")
print(f"EXPORT SUCCESSFUL")
print(f"{'='*50}")
print(f"File : {filename}")
print(f"Sheet : {sheet_name}")
print(f"Records: {len(df)}")
print(f"{'='*50}")
except Exception as e:
print(f"Error exporting to Excel: {e}")
def _adjust_column_widths(self, worksheet) -> None:
"""Auto-fit column widths for readability."""
for column in worksheet.columns:
max_length = 0
column_letter = column[0].column_letter
for cell in column:
try:
if cell.value:
max_length = max(max_length, len(str(cell.value)))
except:
pass
adjusted_width = min(max_length + 2, 60)
worksheet.column_dimensions[column_letter].width = adjusted_width
async def run(self, search_query: str, output_file: str = "gmap_data.xlsx", sheet_name: Optional[str] = None) -> None:
"""Run the full scrape: search results → detail pages → Excel export."""
self.search_query = search_query
self.results = []
self.processed_urls = set()
self.detail_count = 0
try:
await self.setup_crawler()
start_url = f"https://www.google.com/maps/search/{search_query.replace(' ', '+')}"
await self.crawler.run([start_url])
self.export_to_excel(output_file, sheet_name)
except Exception as e:
print(f"Error running scraper: {e}")
def get_user_input() -> str:
"""Interactive prompt for the search query."""
print("\n" + "="*55)
print(" Google Maps Deep Scraper")
print(" (Visits every business page for full details)")
print("="*55)
print("Enter your search query (e.g., 'barber in new york')")
print("Type 'quit' or press Ctrl+C to exit")
print("-"*55)
while True:
query = input("\nSearch query: ").strip()
if query.lower() in ('quit', 'exit', 'q'):
print("Exiting...")
sys.exit(0)
if query:
return query
print("Query cannot be empty. Please try again.")
async def main():
scraper = GoogleMapsScraper(headless=True, timeout_minutes=10)
output_file = "gmap_data.xlsx"
while True:
try:
search_query = get_user_input()
await scraper.run(search_query, output_file)
print("\n" + "-"*55)
again = input("Run another search? (y/n): ").strip().lower()
if again not in ('y', 'yes'):
print("Goodbye!")
break
except KeyboardInterrupt:
print("\n\nInterrupted by user. Exiting...")
break
except Exception as e:
print(f"\nUnexpected error: {e}")
retry = input("Try again? (y/n): ").strip().lower()
if retry not in ('y', 'yes'):
break
if __name__ == "__main__":
asyncio.run(main())