Performance improvements: - Validation speed: 59.71s → 10.96s (5.5x improvement) - Removed 50+ console.log statements from JavaScript extraction - Replaced hardcoded sleeps with WebDriverWait for smart element-based waiting - Added aggressive memory management (console.clear, GC, image unloading every 20 scrolls) Scraping improvements: - Increased idle detection from 6 to 12 consecutive idle scrolls for completeness - Added real-time progress updates every 5 scrolls with percentage calculation - Added crash recovery to extract partial reviews if Chrome crashes - Removed artificial 200-review limit to scrape ALL reviews Timestamp tracking: - Added updated_at field separate from started_at for progress tracking - Frontend now shows both "Started" (fixed) and "Last Update" (dynamic) Robustness improvements: - Added 5 fallback CSS selectors to handle different Google Maps page structures - Now tries: div.jftiEf.fontBodyMedium, div.jftiEf, div[data-review-id], etc. - Automatic selector detection logs which selector works for debugging Test results: - Successfully scraped 550 reviews in 150.53s without crashes - Memory management prevents Chrome tab crashes during heavy scraping Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
126 lines
3.7 KiB
Python
126 lines
3.7 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Test the CSS selector provided by the user to find review count.
|
|
"""
|
|
import time
|
|
from seleniumbase import Driver
|
|
from selenium.webdriver.common.by import By
|
|
|
|
driver = Driver(uc=True, headless=True)
|
|
|
|
url = 'https://www.google.com/maps/search/?api=1&query=instinto+las+palmas&hl=en'
|
|
print(f'Testing with user-provided CSS selector...\n')
|
|
driver.get(url)
|
|
time.sleep(2)
|
|
|
|
# Handle GDPR
|
|
if 'consent.google.com' in driver.current_url:
|
|
form_btns = driver.find_elements(By.CSS_SELECTOR, 'form button')
|
|
for btn in form_btns:
|
|
if 'accept all' in (btn.text or '').lower():
|
|
btn.click()
|
|
time.sleep(2)
|
|
break
|
|
|
|
# Wait for auto-navigation and page load
|
|
time.sleep(6)
|
|
|
|
print(f'Current URL: {driver.current_url[:100]}...\n')
|
|
|
|
# Test the exact selector provided by user
|
|
selector = 'body > div:nth-child(5) > div.lbMcOd.y2iKwd.eZfyae.cSgCkb.xcUKcd.y2Sqzf.Nkjr6c.K1N2o > div.UL7Qtf > div.g2LZJb > div > div > div.w6VYqd > div:nth-child(2) > div > div.e07Vkf.kA9KIf > div > div > div.TIHn2 > div > div.lMbq3e > div.LBgpqf > div > div.fontBodyMedium.dmRWX > div.tos0Ie > div'
|
|
|
|
result = driver.execute_script('''
|
|
const selector = arguments[0];
|
|
const elem = document.querySelector(selector);
|
|
|
|
if (elem) {
|
|
return {
|
|
found: true,
|
|
text: elem.textContent || '',
|
|
innerHTML: elem.innerHTML || '',
|
|
parent: elem.parentElement ? elem.parentElement.textContent : ''
|
|
};
|
|
} else {
|
|
return {
|
|
found: false,
|
|
text: null
|
|
};
|
|
}
|
|
''', selector)
|
|
|
|
print('='*80)
|
|
print('RESULT FROM USER SELECTOR:')
|
|
print('='*80)
|
|
print(f"Found: {result['found']}")
|
|
if result['found']:
|
|
print(f"Text: {result['text']}")
|
|
print(f"HTML: {result['innerHTML'][:200]}")
|
|
print(f"Parent text: {result['parent'][:200]}")
|
|
else:
|
|
print('❌ Element NOT found with that exact selector')
|
|
|
|
# Try simpler selectors based on the classes
|
|
print('\n' + '='*80)
|
|
print('TESTING SIMPLER SELECTORS (key classes from user selector):')
|
|
print('='*80)
|
|
|
|
# Test various class combinations
|
|
selectors_to_test = [
|
|
'div.fontBodyMedium.dmRWX',
|
|
'div.tos0Ie',
|
|
'div.LBgpqf',
|
|
'div.lMbq3e',
|
|
]
|
|
|
|
for test_selector in selectors_to_test:
|
|
elements = driver.execute_script('''
|
|
const selector = arguments[0];
|
|
const elements = document.querySelectorAll(selector);
|
|
const results = [];
|
|
|
|
for (let elem of elements) {
|
|
const text = (elem.textContent || '').trim();
|
|
if (text.length > 0 && text.length < 150) {
|
|
results.push(text);
|
|
}
|
|
}
|
|
|
|
return results.slice(0, 5); // First 5 matches
|
|
''', test_selector)
|
|
|
|
print(f'\nSelector: {test_selector}')
|
|
print(f'Found {len(elements)} element(s):')
|
|
for i, text in enumerate(elements, 1):
|
|
print(f' {i}. {text[:100]}')
|
|
|
|
# Also look for any element containing "review" in these specific class contexts
|
|
print('\n' + '='*80)
|
|
print('SEARCHING FOR REVIEW COUNT IN SIMILAR LOCATIONS:')
|
|
print('='*80)
|
|
|
|
review_search = driver.execute_script('''
|
|
const results = [];
|
|
|
|
// Look for elements with classes that might contain review info
|
|
const candidates = document.querySelectorAll('div.fontBodyMedium, div[class*="dmRWX"], div[class*="tos0Ie"]');
|
|
|
|
for (let elem of candidates) {
|
|
const text = (elem.textContent || '').trim();
|
|
if (text.length > 0 && text.length < 200 && /review|reseña/i.test(text)) {
|
|
results.push({
|
|
text: text,
|
|
classes: elem.className
|
|
});
|
|
}
|
|
}
|
|
|
|
return results.slice(0, 10);
|
|
''')
|
|
|
|
for i, item in enumerate(review_search, 1):
|
|
print(f"\n{i}. Classes: {item['classes'][:80]}")
|
|
print(f" Text: {item['text'][:100]}")
|
|
|
|
driver.quit()
|