-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscraper.py
More file actions
1416 lines (1208 loc) · 56 KB
/
scraper.py
File metadata and controls
1416 lines (1208 loc) · 56 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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Core Scraper Engine for Google Maps
===================================
Playwright-based scraper optimized for Category 1 (list-view) data extraction.
Includes bandwidth optimization for 1GB free proxy tier.
"""
import re
import time
import logging
import hashlib
from datetime import datetime
from typing import Optional, List, Dict, Any, Callable
from urllib.parse import urlparse, parse_qs
from playwright.sync_api import (
sync_playwright,
Page,
Browser,
BrowserContext,
Route,
TimeoutError as PlaywrightTimeout
)
from pathlib import Path
from db import DatabaseManager
logger = logging.getLogger(__name__)
PROFILE_DIR = Path(__file__).parent / ".gmap_profile"
class GMapScraper:
"""
Google Maps scraper for Category 1 (list-view metadata) extraction.
Features:
- Bandwidth optimization (blocks images/fonts/CSS)
- Coordinate extraction from URLs
- Google ID parsing (place_id, data_id, cid)
- Robust error handling per-listing
"""
def __init__(self, config: dict, db: DatabaseManager):
self.config = config
self.db = db
self.browser: Optional[Browser] = None
self.context: Optional[BrowserContext] = None
self.page: Optional[Page] = None
# Callbacks for progress reporting
self.on_city_start: Optional[Callable] = None
self.on_city_complete: Optional[Callable] = None
self.on_city_error: Optional[Callable] = None
self.on_listing_found: Optional[Callable] = None
self.on_business_inserted: Optional[Callable] = None # Called with (business_data, is_new)
self.on_bandwidth_used: Optional[Callable] = None # Called with (bytes_used)
# Bandwidth tracking
self._bandwidth_bytes = 0
# Cross-scrape dedup: set of hashes to exclude
self.excluded_hashes: set = set()
def _block_media(self, route: Route):
"""
Bandwidth optimization: abort non-essential resource requests.
Essential for staying within 1GB free proxy limit.
Also tracks bandwidth usage.
"""
resource_type = route.request.resource_type
blocked_types = []
if self.config.get('optimization', {}).get('block_images', True):
blocked_types.append('image')
if self.config.get('optimization', {}).get('block_fonts', True):
blocked_types.append('font')
if self.config.get('optimization', {}).get('block_stylesheets', True):
blocked_types.append('stylesheet')
if self.config.get('optimization', {}).get('block_media', True):
blocked_types.append('media')
if resource_type in blocked_types:
route.abort()
else:
route.continue_()
def _track_response(self, response):
"""Track bandwidth from responses."""
try:
# Get response body size (if available)
headers = response.headers
content_length = headers.get('content-length', '0')
bytes_used = int(content_length) if content_length.isdigit() else 0
# Estimate from URL if no content-length
if bytes_used == 0:
# Rough estimate: 5KB average per request
bytes_used = 5000
self._bandwidth_bytes += bytes_used
if self.on_bandwidth_used:
self.on_bandwidth_used(bytes_used)
except:
pass
def get_bandwidth_used(self) -> int:
"""Get total bandwidth used in bytes."""
return self._bandwidth_bytes
def reset_bandwidth(self):
"""Reset bandwidth counter."""
self._bandwidth_bytes = 0
def _parse_coordinates(self, url: str) -> tuple:
"""
Extract latitude/longitude from Google Maps URL.
Primary pattern (data URLs): !3d{lat}!4d{lng}
Example: !3d32.9545907!4d-96.8375646 -> (32.9545907, -96.8375646)
Fallback pattern: @{lat},{lng},{zoom}z
"""
if not url:
return None, None
# Primary: data URL format used in place URLs with rclk=1
match = re.search(r'!3d(-?\d+\.\d+)!4d(-?\d+\.\d+)', url)
if match:
return float(match.group(1)), float(match.group(2))
# Fallback: classic @lat,lng format
match = re.search(r'@(-?\d+\.\d+),(-?\d+\.\d+)', url)
if match:
return float(match.group(1)), float(match.group(2))
return None, None
def _parse_google_ids(self, url: str) -> dict:
"""
Extract Google identifiers from Maps URL.
Returns:
{
'place_id': 'ChIJ...', # Permanent Google Place ID (!19s)
'data_id': '0x...:0x...', # Hex compound identifier (!1s)
'cid': '123...', # Customer ID (decimal, from cid= param)
'google_feature_id': '/g/...' # Feature ID (!16s)
}
"""
from urllib.parse import unquote
ids = {'place_id': None, 'data_id': None, 'cid': None, 'google_feature_id': None}
if not url:
return ids
# Place ID: !19sChIJ... format in data URLs
place_match = re.search(r'!19s(ChIJ[A-Za-z0-9_\-]+)', url)
if place_match:
ids['place_id'] = place_match.group(1)
# Data ID: Compound hex format 0x...:0x...
data_match = re.search(r'(0x[a-f0-9]+:0x[a-f0-9]+)', url)
if data_match:
ids['data_id'] = data_match.group(1)
# CID: Usually in query params or embedded in URL
cid_match = re.search(r'cid[=:](\d+)', url)
if cid_match:
ids['cid'] = cid_match.group(1)
# Feature ID: !16s%2Fg%2F... (URL-encoded /g/XXXXXXXXX)
feature_match = re.search(r'!16s(%2[Ff]g%2[Ff][^!?&]+)', url)
if feature_match:
ids['google_feature_id'] = unquote(feature_match.group(1))
return ids
def _parse_rating_text(self, text: str) -> tuple:
"""
Parse rating and review count from text.
Formats handled:
- "4.8(154)" or "4.9(3,986) · $20–30" (visible browser mode)
- "4.8" or "4.9" (headless browser mode - just rating, no review count)
Returns: (rating: float, review_count: int or None, raw_text: str)
"""
if not text:
return None, None, None
text = text.strip()
# Pattern 1: "X.X(NUMBER)" - handles commas in review count like "4.9(3,986)"
match = re.search(r'(\d+\.?\d*)\s*\(([\d,]+)', text)
if match:
rating = float(match.group(1))
# Remove commas from review count before converting to int
count_str = match.group(2).replace(',', '')
count = int(count_str)
return rating, count, text
# Pattern 2: Standalone rating "4.8" or "4.9" (headless mode)
# Must be a valid rating (1.0-5.0 range) and standalone or at start of line
rating_match = re.match(r'^(\d+\.\d)$', text)
if rating_match:
rating = float(rating_match.group(1))
# Valid Google ratings are 1.0-5.0
if 1.0 <= rating <= 5.0:
return rating, None, text
return None, None, None
def _parse_phone(self, text_lines: List[str]) -> Optional[str]:
"""
Extract phone number from listing text content.
Looks for patterns like: +1 XXX-XXX-XXXX, (XXX) XXX-XXXX, etc.
"""
phone_patterns = [
r'\+1[\s\-]?\(?\d{3}\)?[\s\-]?\d{3}[\s\-]?\d{4}', # +1 format
r'\(\d{3}\)\s?\d{3}[\s\-]?\d{4}', # (XXX) format
r'\d{3}[\s\-]\d{3}[\s\-]\d{4}', # XXX-XXX-XXXX
]
for line in text_lines:
for pattern in phone_patterns:
match = re.search(pattern, line)
if match:
return match.group(0)
return None
def extract_category_one(self, page: Page, search_query: str, search_city: str) -> List[Dict[str, Any]]:
"""
Extract Category 1 metadata from all visible listings.
Category 1 = Data visible in the list/grid view without clicking.
This is the fastest extraction method and uses minimal bandwidth.
"""
results = []
try:
# Get all listing articles in the feed
listings = page.locator('div[role="article"]').all()
logger.debug(f"Found {len(listings)} potential listings")
for rank, listing in enumerate(listings, start=1):
try:
data = self._extract_single_listing(listing, rank, search_query, search_city)
if data and data.get('name'):
results.append(data)
if self.on_listing_found:
self.on_listing_found(data)
except Exception as e:
logger.debug(f"Failed to extract listing {rank}: {e}")
continue
except Exception as e:
logger.error(f"Error extracting listings: {e}")
return results
def _extract_single_listing(
self,
listing,
rank: int,
search_query: str,
search_city: str
) -> Optional[Dict[str, Any]]:
"""Extract data from a single listing element."""
data = {
'rank_on_page': rank,
'search_query': search_query,
'search_city': search_city,
'scraped_at': datetime.now().isoformat()
}
# 1. Get the main link (contains name and URL)
link_locator = listing.locator('a[href*="google.com/maps"]')
if link_locator.count() == 0:
# Try alternate selector
link_locator = listing.locator('a[href*="maps.google"]')
if link_locator.count() > 0:
url = link_locator.first.get_attribute('href')
name = link_locator.first.get_attribute('aria-label')
data['google_url'] = url
data['name'] = name
# Parse coordinates from URL
lat, lng = self._parse_coordinates(url)
data['latitude'] = lat
data['longitude'] = lng
# Parse Google IDs from URL
ids = self._parse_google_ids(url)
data.update(ids)
else:
return None # No link = not a valid listing
# Derive state and city_name from search_city ("Dallas, TX" -> "TX", "Dallas")
if search_city and ',' in search_city:
parts = [p.strip() for p in search_city.split(',')]
data['city_name'] = parts[0]
data['state'] = parts[-1]
else:
data['city_name'] = search_city
data['state'] = None
# 2. Parse text content for phone, rating, address, price
try:
text_content = listing.inner_text()
text_lines = [line.strip() for line in text_content.split('\n') if line.strip()]
# Phone number
data['phone_number'] = self._parse_phone(text_lines)
# Rating and review count
# Format varies:
# "4.8(255)" — combined (no CSS blocking)
# "4.8" then "(55)" on next line — split (CSS blocked)
# "4.8" alone — headless/limited view
for idx, line in enumerate(text_lines):
rating, count, raw = self._parse_rating_text(line)
if rating is not None:
data['rating'] = rating
data['review_count'] = count
data['rating_text_raw'] = raw
# If count is missing, check next line for "(N)" pattern
if count is None and idx + 1 < len(text_lines):
next_line = text_lines[idx + 1].strip()
count_match = re.match(r'^\(([\d,]+)\)$', next_line)
if count_match:
data['review_count'] = int(count_match.group(1).replace(',', ''))
data['rating_text_raw'] = f"{line}{next_line}"
break
# Review count fallback: aria-label on the stars/rating element
# Google renders: aria-label="Rated 4.9 out of 5, 255 reviews"
if not data.get('review_count'):
try:
stars_el = listing.locator('[aria-label*="out of 5"], [aria-label*="stars"]')
if stars_el.count() > 0:
aria = stars_el.first.get_attribute('aria-label') or ''
review_match = re.search(r'([\d,]+)\s*review', aria, re.IGNORECASE)
if review_match:
data['review_count'] = int(review_match.group(1).replace(',', ''))
if not data.get('rating'):
rating_match = re.search(r'(\d+\.?\d*)\s*(?:out of|stars)', aria, re.IGNORECASE)
if rating_match:
data['rating'] = float(rating_match.group(1))
except Exception:
pass
# Category, Address, and Price parsing
# Google Maps format: "Category · [icon] · Address" on one line
# May also include price level: "Smoke shop · $ · 123 Main St"
name_lower = data.get('name', '').lower() if data.get('name') else ''
for line in text_lines:
# Skip lines that are clearly not category/address
if '(' in line and ')' in line: # Rating line
continue
if line.lower() == name_lower: # Skip the name itself
continue
if any(skip in line.lower() for skip in ['open', 'closed', '+1', 'www.', 'http']):
continue
# Look for lines with · separator (category · address pattern)
if '·' in line:
parts = [p.strip() for p in line.split('·')]
# Filter out empty parts and icon characters (like \ue934)
clean_parts = []
price_parts = []
for p in parts:
# Skip empty or single Unicode icon chars
if not p or (len(p) == 1 and ord(p[0]) > 0xe000):
continue
# Capture price indicators separately ("$", "$$", "$$$", "$$$$")
if re.match(r'^\$+$', p.strip()):
price_parts.append(p.strip())
continue
# Skip price ranges like "$20–30"
if p.startswith('$') and len(p) > 2:
continue
clean_parts.append(p)
if price_parts and not data.get('price_level'):
data['price_level'] = price_parts[0]
if len(clean_parts) >= 1:
# First clean part is usually the category
if not data.get('category'):
data['category'] = clean_parts[0]
# Look for address in remaining parts
address_patterns = ['st', 'ave', 'blvd', 'rd', 'dr', 'ln', 'way', 'hwy', 'pkwy', 'suite', 'ste']
for part in clean_parts[1:]:
part_lower = part.lower()
if any(p in part_lower for p in address_patterns) or re.search(r'\d+', part):
data['address_text'] = part
break
continue
# Fallback: look for address in any line if not found
if not data.get('address_text'):
address_patterns = ['st', 'ave', 'blvd', 'rd', 'dr', 'ln', 'way', 'hwy', 'pkwy']
for line in text_lines:
line_lower = line.lower()
if any(pattern in line_lower for pattern in address_patterns):
if line.lower() != name_lower:
data['address_text'] = line
break
# Extract zip code from address text
if data.get('address_text'):
zip_match = re.search(r'\b(\d{5})(?:-\d{4})?\b', data['address_text'])
if zip_match:
data['zip_code'] = zip_match.group(1)
# Open/closed status
for line in text_lines:
line_lower = line.lower()
if 'open' in line_lower or 'closed' in line_lower:
data['open_status'] = line
# Check for hours snippet (e.g., "Closes 5 PM")
if 'closes' in line_lower or 'opens' in line_lower:
data['hours_snippet'] = line
break
# Service options (e.g. "Delivery · In-store pickup · No-contact delivery")
service_keywords = ['delivery', 'dine-in', 'takeout', 'takeaway', 'pickup', 'no-contact', 'curbside']
for line in text_lines:
line_lower = line.lower()
if any(kw in line_lower for kw in service_keywords):
data['service_options'] = line
break
# Description snippet: any remaining line that doesn't fit other categories
# e.g. "Hookahs, pipes, vaporizers & accessories" or "Women-led"
skip_patterns = ['open', 'closed', 'closes', 'opens', '+1', '(']
cat_lower = (data.get('category', '') or '').lower()
addr_lower = (data.get('address_text', '') or '').lower()
name_lower_set = {
data.get('name', '').lower(),
cat_lower,
addr_lower,
(data.get('open_status', '') or '').lower(),
(data.get('hours_snippet', '') or '').lower(),
(data.get('service_options', '') or '').lower(),
(data.get('phone_number', '') or '').lower(),
}
for line in text_lines:
line_lower = line.lower()
if line_lower in name_lower_set:
continue
if any(p in line_lower for p in skip_patterns):
continue
if re.match(r'^\d+\.?\d*$', line): # pure number (rating)
continue
# Skip the combined "Category · icon · Address" line
if cat_lower and cat_lower in line_lower:
continue
if addr_lower and addr_lower in line_lower:
continue
# It's an unaccounted-for text line — likely a description/highlight
data['description_snippet'] = line
break
except Exception as e:
logger.debug(f"Error parsing text content: {e}")
# 3. Accessibility tags (e.g. "Wheelchair accessible entrance")
try:
accessibility_els = listing.locator('[aria-label*="accessible" i], [aria-label*="wheelchair" i]').all()
if accessibility_els:
tags = []
for el in accessibility_els:
lbl = el.get_attribute('aria-label') or ''
if lbl and lbl not in tags:
tags.append(lbl)
if tags:
data['accessibility'] = '; '.join(tags)
except Exception:
pass
# 4. Website URL — list view rarely shows a website button, but try common selectors
try:
for selector in [
'a[data-value="Website"]',
'a[jsaction*="website"]',
'a[aria-label*="Website"]',
'a[aria-label*="website"]',
]:
btn = listing.locator(selector)
if btn.count() > 0:
href = btn.first.get_attribute('href')
if href and 'google.com' not in href:
data['website_url'] = href
break
except Exception:
pass
# 4. Thumbnail URL
try:
img = listing.locator('img').first
if img.count() > 0:
data['thumbnail_url'] = img.get_attribute('src')
except:
pass
return data
def extract_deep_details(self, listing, timeout_ms: int = 5000) -> Dict[str, Any]:
"""
Extract detailed info by clicking into a listing's detail panel.
This extracts Category 2 data (phone, website, full address, hours)
that is only visible in the detail view.
Args:
listing: The listing element to click
timeout_ms: Timeout for waiting for detail panel
Returns:
Dict with phone_number, website_url, full_address, hours, and other details
"""
details = {}
try:
# Click the listing to open detail panel
listing.scroll_into_view_if_needed()
time.sleep(0.2)
listing.click()
# Wait for detail panel to load (look for action buttons)
try:
self.page.wait_for_selector(
'button[data-item-id^="phone"], a[data-item-id="authority"], div[role="main"]',
timeout=timeout_ms
)
time.sleep(0.5) # Let content settle
except:
return details
# Extract phone number from detail panel
# Note: data-item-id format changed from "phone" to "phone:tel:+1234567890"
try:
phone_btn = self.page.locator('button[data-item-id^="phone"]')
if phone_btn.count() > 0:
# Phone is in aria-label like "Phone: (214) 555-1234"
aria = phone_btn.first.get_attribute('aria-label')
if aria and 'phone' in aria.lower():
# Extract phone from "Phone: (XXX) XXX-XXXX"
phone_match = re.search(
r'[\+]?[\d\s\-\(\)]{10,}',
aria
)
if phone_match:
details['phone_number'] = phone_match.group(0).strip()
except Exception as e:
logger.debug(f"Error extracting phone: {e}")
# Extract website URL
try:
website_link = self.page.locator('a[data-item-id="authority"]')
if website_link.count() > 0:
href = website_link.first.get_attribute('href')
if href:
details['website_url'] = href
except Exception as e:
logger.debug(f"Error extracting website: {e}")
# Extract full address
try:
address_btn = self.page.locator('button[data-item-id="address"]')
if address_btn.count() > 0:
aria = address_btn.first.get_attribute('aria-label')
if aria and 'address' in aria.lower():
# Remove "Address: " prefix
details['full_address'] = aria.replace('Address:', '').strip()
except Exception as e:
logger.debug(f"Error extracting address: {e}")
# Extract hours
try:
hours_btn = self.page.locator('button[data-item-id*="oh"]')
if hours_btn.count() > 0:
aria = hours_btn.first.get_attribute('aria-label')
if aria:
details['hours_text'] = aria
except Exception as e:
logger.debug(f"Error extracting hours: {e}")
# Extract review count if not already captured
try:
review_btn = self.page.locator('button[jsaction*="review"]')
if review_btn.count() > 0:
text = review_btn.first.inner_text()
count_match = re.search(r'([\d,]+)\s*review', text, re.IGNORECASE)
if count_match:
details['review_count'] = int(count_match.group(1).replace(',', ''))
except:
pass
# Go back to results list
try:
back_btn = self.page.locator('button[jsaction*="back"], button[aria-label="Back"]')
if back_btn.count() > 0:
back_btn.first.click()
time.sleep(0.3)
else:
# Press escape to close panel
self.page.keyboard.press('Escape')
time.sleep(0.3)
except:
self.page.keyboard.press('Escape')
except Exception as e:
logger.debug(f"Error in deep extraction: {e}")
try:
self.page.keyboard.press('Escape')
except:
pass
return details
def _enrich_listings(self, table_name: str, query: str, city: str):
"""
Enrich listings with website URL and full address by navigating directly
to each business's Google Maps page (avoids stale-element issues with
the click-in-list-view approach).
Uses confirmed selectors from live DOM inspection:
- website: a[data-item-id="authority"]
- phone: button[data-item-id^="phone"] (data-item-id is "phone:tel:+1...")
- address: button[data-item-id="address"]
- plus code: button[aria-label*="Plus code"]
"""
try:
cursor = self.db.conn.execute(
f'SELECT dedup_hash, name, google_url, phone_number, zip_code, plus_code, review_count '
f'FROM "{table_name}" '
f'WHERE (website_url IS NULL OR website_url = \'\') '
f'AND search_city = ? AND google_url IS NOT NULL',
(city,)
)
businesses = cursor.fetchall()
except Exception as e:
logger.debug(f"Could not query for website enrichment: {e}")
return
if not businesses:
return
logger.info(f"Fetching websites for {len(businesses)} businesses in {city}")
for dedup_hash, name, google_url, existing_phone, existing_zip, existing_plus, existing_review_count in businesses:
try:
self.page.goto(google_url, timeout=12000, wait_until='domcontentloaded')
time.sleep(1.2)
updates = {}
# Website: a[data-item-id="authority"]
website_el = self.page.locator('a[data-item-id="authority"]')
if website_el.count() > 0:
href = website_el.first.get_attribute('href') or ''
if href and 'google.com' not in href:
updates['website_url'] = href
# Full address: button[data-item-id="address"]
# aria-label = "Address: 4620 Washington Ave Suite A, Houston, TX 77007"
addr_el = self.page.locator('button[data-item-id="address"]')
if addr_el.count() > 0:
aria = addr_el.first.get_attribute('aria-label') or ''
if aria:
full_addr = re.sub(r'^Address:\s*', '', aria).strip()
updates['address_text'] = full_addr
# Extract zip code from full address if not already present
if not existing_zip:
zip_match = re.search(r'\b(\d{5})(?:-\d{4})?\b', full_addr)
if zip_match:
updates['zip_code'] = zip_match.group(1)
# Plus code (only if missing)
if not existing_plus:
plus_el = self.page.locator('button[aria-label*="Plus code"]')
if plus_el.count() > 0:
aria = plus_el.first.get_attribute('aria-label') or ''
if 'Plus code' in aria:
updates['plus_code'] = re.sub(r'^Plus code:\s*', '', aria).strip()
# Phone fallback (only if missing from list-view)
if not existing_phone:
phone_el = self.page.locator('button[data-item-id^="phone"]')
if phone_el.count() > 0:
aria = phone_el.first.get_attribute('aria-label') or ''
phone_match = re.search(r'[\+]?[\d\s\-\(\)]{10,}', aria)
if phone_match:
updates['phone_number'] = phone_match.group(0).strip()
# Review count fallback from detail page
if not existing_review_count:
try:
review_btn = self.page.locator('button[jsaction*="reviewChart"], button[aria-label*="review"]')
if review_btn.count() > 0:
rev_text = review_btn.first.inner_text()
rev_match = re.search(r'([\d,]+)\s*review', rev_text, re.IGNORECASE)
if rev_match:
updates['review_count'] = int(rev_match.group(1).replace(',', ''))
if 'review_count' not in updates:
# Try aria-label on rating element: "Rated 4.9 out of 5, 255 reviews"
stars_el = self.page.locator('[aria-label*="out of 5"]')
if stars_el.count() > 0:
aria = stars_el.first.get_attribute('aria-label') or ''
rev_match = re.search(r'([\d,]+)\s*review', aria, re.IGNORECASE)
if rev_match:
updates['review_count'] = int(rev_match.group(1).replace(',', ''))
except Exception:
pass
if updates:
set_clause = ', '.join(f'{k} = ?' for k in updates)
values = list(updates.values()) + [dedup_hash]
self.db.conn.execute(
f'UPDATE "{table_name}" SET {set_clause} WHERE dedup_hash = ?',
values
)
self.db.conn.commit()
except Exception as e:
logger.debug(f"Website enrich error for {name}: {e}")
continue
def scrape_city_deep(
self,
query: str,
city: str,
table_name: str,
max_scrolls: int = None,
preset: str = None,
on_deep_progress: Callable = None
) -> tuple:
"""
Scrape a city with deep extraction (clicks into each listing for phone/website).
This is slower but extracts complete data including phone numbers and websites.
Returns: (results_count, error_message)
"""
if not self.page:
raise RuntimeError("Browser not started. Call start_browser() first.")
full_query = f"{query} in {city}"
scrape_config = self.config.get('scrape', {})
max_scrolls = max_scrolls or scrape_config.get('scroll_limit', 10)
scroll_delay = scrape_config.get('scroll_delay_ms', 1500) / 1000
timeout = scrape_config.get('page_load_timeout_ms', 15000)
if self.on_city_start:
self.on_city_start(city, query)
try:
# Navigate directly to search URL (more reliable than filling search box)
self._navigate_search(query, city, timeout)
time.sleep(2)
# Scroll to load all results first
feed = self.page.locator('div[role="feed"]')
prev_count = 0
stale_scrolls = 0
for scroll_num in range(max_scrolls):
try:
feed.evaluate("el => el.scrollTo(0, el.scrollHeight)")
time.sleep(scroll_delay)
# Check for end of results
end_indicator = self.page.locator("text=You've reached the end")
if end_indicator.count() > 0:
break
# Detect stale scrolling
cur_count = self.page.locator('div[role="article"]').count()
if cur_count == prev_count:
stale_scrolls += 1
if stale_scrolls >= 3:
break
else:
stale_scrolls = 0
prev_count = cur_count
except:
break
# Now extract all listings with deep details
listings = self.page.locator('div[role="article"]').all()
total_listings = len(listings)
logger.info(f"Found {total_listings} listings for deep extraction")
seen_hashes = set()
inserted = 0
for i, listing in enumerate(listings):
try:
# First get basic info
basic_data = self._extract_single_listing(listing, i + 1, query, city)
if not basic_data or not basic_data.get('name'):
continue
# Check for duplicates
unique_str = f"{basic_data.get('name', '')}{basic_data.get('google_url', '')}"
dedup_hash = hashlib.md5(unique_str.encode()).hexdigest()
if dedup_hash in seen_hashes:
continue
seen_hashes.add(dedup_hash)
# Check cross-scrape exclusion
if dedup_hash in self.excluded_hashes:
is_new = False
else:
# Now get deep details (this is the slow part)
deep_data = self.extract_deep_details(listing)
# Merge deep data into basic data (deep data takes priority)
if deep_data.get('phone_number'):
basic_data['phone_number'] = deep_data['phone_number']
if deep_data.get('website_url'):
basic_data['website_url'] = deep_data['website_url']
if deep_data.get('full_address'):
basic_data['full_address'] = deep_data['full_address']
if deep_data.get('hours_text'):
basic_data['hours_text'] = deep_data['hours_text']
if deep_data.get('review_count') and not basic_data.get('review_count'):
basic_data['review_count'] = deep_data['review_count']
basic_data['dedup_hash'] = dedup_hash
# Insert immediately and report progress
is_new = self.db.insert_result(table_name, basic_data)
if is_new:
inserted += 1
# Real-time progress callback for live monitor
if self.on_business_inserted:
self.on_business_inserted(basic_data, is_new)
# Report progress
if on_deep_progress:
on_deep_progress(i + 1, total_listings, basic_data)
except Exception as e:
logger.debug(f"Error processing listing {i}: {e}")
continue
if self.on_city_complete:
self.on_city_complete(city, inserted, len(seen_hashes))
return inserted, None
except PlaywrightTimeout:
error_msg = f"Timeout loading results for {city}"
logger.warning(error_msg)
return 0, error_msg
except Exception as e:
error_msg = str(e)
logger.error(f"Error scraping {city}: {error_msg}")
return 0, error_msg
def enrich_table_with_deep_data(
self,
table_name: str,
limit: int = None,
on_progress: Callable = None
) -> Dict[str, int]:
"""
Enrich an existing table with phone numbers and websites using deep extraction.
Finds businesses missing phone/website and looks them up on Google Maps.
Args:
table_name: Table to enrich
limit: Max businesses to enrich (None = all)
on_progress: Callback(current, total, business_name)
Returns:
Dict with enrichment statistics
"""
stats = {
'total_processed': 0,
'phones_found': 0,
'websites_found': 0,
'errors': 0
}
# Get businesses missing phone or website
query = f"""
SELECT dedup_hash, name, google_url, search_city
FROM "{table_name}"
WHERE (phone_number IS NULL OR phone_number = '')
OR (website_url IS NULL OR website_url = '')
"""
if limit:
query += f" LIMIT {limit}"
cursor = self.db.conn.execute(query)
businesses = cursor.fetchall()
if not businesses:
logger.info("No businesses need enrichment")
return stats
logger.info(f"Enriching {len(businesses)} businesses in {table_name}")
if not self.page:
self.start_browser()
for i, (dedup_hash, name, google_url, city) in enumerate(businesses):
try:
if on_progress:
on_progress(i + 1, len(businesses), name)
# Navigate directly to the business
if google_url:
self.page.goto(google_url, timeout=15000)
time.sleep(1.5)
# Extract details from detail view
details = {}
# Phone
try:
phone_btn = self.page.locator('button[data-item-id="phone"]')
if phone_btn.count() > 0:
aria = phone_btn.first.get_attribute('aria-label')
if aria:
phone_match = re.search(r'[\+]?[\d\s\-\(\)]{10,}', aria)
if phone_match:
details['phone_number'] = phone_match.group(0).strip()
stats['phones_found'] += 1
except:
pass
# Website
try:
website_link = self.page.locator('a[data-item-id="authority"]')
if website_link.count() > 0:
href = website_link.first.get_attribute('href')
if href:
details['website_url'] = href
stats['websites_found'] += 1
except:
pass
# Update database
if details:
updates = []
values = []
if details.get('phone_number'):
updates.append("phone_number = ?")
values.append(details['phone_number'])
if details.get('website_url'):
updates.append("website_url = ?")
values.append(details['website_url'])
if updates:
values.append(dedup_hash)
self.db.conn.execute(
f"UPDATE \"{table_name}\" SET {', '.join(updates)} WHERE dedup_hash = ?",
values
)
self.db.conn.commit()
stats['total_processed'] += 1
except Exception as e:
logger.debug(f"Error enriching {name}: {e}")
stats['errors'] += 1
continue
return stats
# Path for exported cookies (shared across workers)
COOKIES_FILE = PROFILE_DIR / "cookies.json"
@staticmethod
def has_google_login() -> bool:
"""Check if a Google-authenticated browser profile exists."""
return PROFILE_DIR.exists() and (PROFILE_DIR / "Default").exists()
@classmethod
def export_cookies(cls):
"""Export cookies from persistent profile to a JSON file for parallel workers."""
if not cls.has_google_login():
return
import json
pw = sync_playwright().start()
ctx = pw.chromium.launch_persistent_context(