Repository navigation
Expand file tree
/
Copy pathselenium_scraper.py
More file actions
459 lines (401 loc) · 20.2 KB
/
Copy pathselenium_scraper.py
File metadata and controls
459 lines (401 loc) · 20.2 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
#!/usr/bin/env python3
"""selenium_scraper.py — Selenium engine, parity copy of
playwright_scraper.py (same flags, same exit codes, same status semantics
— see output_writer.finish_run). Selenium is not the primary engine
(Playwright is); it exists for parity, not because it is preferred.
Same hard engine limits as the rest of the family (stockx-scraper /
skyscanner-scraper's selenium_scraper.py):
- Selenium CANNOT use an AUTHENTICATED remote CDP endpoint.
chromedriver's `debuggerAddress` takes a bare `host:port`; Playwright's
`connect_over_cdp` and pyppeteer's `connect` take a full
`ws://user:pass@host:port` and authenticate on the WebSocket upgrade.
A `--cdp-endpoint` carrying credentials (the Scraping Browser API
shape) is refused outright here with EXIT_BAD_USAGE — no half-working
attempt — with a pointer to playwright_scraper.py / puppeteer_scraper.py.
- Selenium's `--proxy-server` CANNOT authenticate at all. A `--proxy`
with a login/password has its credentials stripped before being handed
to Chrome, and this engine WARNS rather than silently dropping them.
Chrome binary: normally auto-detected by Selenium/Selenium Manager from a
regular Chrome/Chromium install. Set `CHROME_BIN` or `SELENIUM_CHROME_BIN`
to point at a specific binary instead; `SELENIUM_CHROMEDRIVER_PATH` /
`CHROMEDRIVER_PATH` pin a specific chromedriver (Selenium Manager's
default network auto-resolve fails outright in an offline/network-
restricted environment — see the constant below).
"""
from __future__ import annotations
import argparse
import logging
import os
import sys
import time
from pathlib import Path
from typing import List, Optional, Tuple
from urllib.parse import urlparse
try:
from selenium import webdriver
from selenium.common.exceptions import WebDriverException
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
except ImportError as _IMPORT_ERROR: # pragma: no cover — exercised by smoke_test's no-engine path
webdriver = None
WebDriverException = Exception
Options = None
Service = None
_SELENIUM_IMPORT_ERROR = _IMPORT_ERROR
else:
_SELENIUM_IMPORT_ERROR = None
import env_config
import lidl_parser as lp
import run_policy as rp
from captcha_solver import SolveBudget, detect_from_html
from lidl_stores import Store, StoreApiError
from output_writer import EXIT_BAD_USAGE, EXIT_CRASH, EXIT_REMOTE_API_ERROR, Product, finish_run, sku_key as _sku_key
from proxy_pool import Proxy, ProxyPool, ProxyParseError, is_proxy_dead_error, load_proxies
from fingerprint_client import fetch_fingerprint, refuse_if_cdp, user_agent_from
from scraper_api_client import TwoCaptchaClient
ENGINE_NAME = "selenium"
NAV_TIMEOUT_S = 30
READINESS_WAIT_S = 3.0
_CHROME_BINARY = os.environ.get("SELENIUM_CHROME_BIN") or os.environ.get("CHROME_BIN")
# Selenium 4's Selenium Manager resolves/downloads a matching chromedriver
# over the network by default (googlechromelabs.github.io) whenever
# Service() is called with no explicit path — which fails outright in any
# network-restricted environment (a CI runner with no outbound internet,
# an air-gapped box) even when a compatible chromedriver is already
# installed and on PATH. Mirrors playwright's PLAYWRIGHT_BROWSERS_PATH and
# puppeteer's PYPPETEER_EXECUTABLE_PATH/PUPPETEER_EXECUTABLE_PATH escape
# hatches so this engine isn't the one that can never run offline.
_CHROMEDRIVER_PATH = os.environ.get("SELENIUM_CHROMEDRIVER_PATH") or os.environ.get("CHROMEDRIVER_PATH")
# Selenium Manager phones home to plausible.io with usage stats by default
# (confirmed live on stockx-scraper/skyscanner-scraper) — opted out the
# same way here, before this module's own SECURITY.md promise is tested.
os.environ.setdefault("SE_AVOID_STATS", "true")
log = logging.getLogger("selenium_scraper")
def build_arg_parser() -> argparse.ArgumentParser:
p = argparse.ArgumentParser(
description="lidl.com grocery product scraper — Selenium engine",
epilog="Credentials belong in .env / LIDL_PROXY / TWOCAPTCHA_KEY — never on this command line.",
)
p.add_argument("--url", default=None)
p.add_argument("--query", default=None)
p.add_argument("--category", default=None)
p.add_argument("--zip", dest="zip_code", default=None)
p.add_argument("--store-id", default=None)
p.add_argument("--sort", choices=lp.SORT_VALUES, default="relevance")
p.add_argument("--max-results", type=rp.positive_int, default=30)
p.add_argument("--max-scrolls", type=rp.positive_int, default=None)
p.add_argument("--stall-rounds", type=rp.positive_int, default=4)
p.add_argument("--scroll-delay", type=rp.nonnegative_float, default=1.5)
p.add_argument("--format", choices=["json", "csv"], default="json")
p.add_argument("--out", default=None)
p.add_argument("--retries", type=rp.nonnegative_int, default=2)
p.add_argument("--retry-delay", type=rp.nonnegative_float, default=3.0)
p.add_argument("--proxy", default=None)
p.add_argument("--proxy-file", default=None)
p.add_argument("--proxy-shuffle", action="store_true")
p.add_argument("--proxy-block-retries", type=rp.nonnegative_int, default=3)
p.add_argument("--twocaptcha-key", default=None)
p.add_argument("--captcha-api", default=None, help="Override the 2Captcha API base URL (testing only)")
p.add_argument("--solve-captcha", choices=["off", "when-blocked", "always"], default="when-blocked")
p.add_argument("--min-score", type=rp.unit_float, default=0.3, help="Minimum acceptable reCAPTCHA v3 score (2Captcha's minScore task field)")
p.add_argument("--fingerprint", action="store_true", help="Fetch and apply a 2Captcha Fingerprint API profile's user agent (ignored with --cdp-endpoint — see fingerprint_client.refuse_if_cdp)")
p.add_argument("--fp-tags", default=None, help="Fingerprint API filter, e.g. 'Windows'")
p.add_argument("--fp-country", default=None, help="Fingerprint API filter, e.g. 'us'")
p.add_argument("--cdp-endpoint", default=None,
help="NOTE: refused if it carries credentials — Selenium cannot authenticate a remote CDP session")
p.add_argument("--allow-empty", action="store_true")
p.add_argument("--dump-html", action="store_true")
p.add_argument("--headless", dest="headless", action="store_true", default=True)
p.add_argument("--headful", dest="headless", action="store_false")
return p
def _default_out(fmt: str) -> str:
return f"lidl_results.{fmt}"
def _resolve_start_url(args: argparse.Namespace) -> Optional[str]:
return rp.resolve_start_url(args)
def _quit_quietly(driver) -> None:
"""Quit a driver whose browser may already be gone."""
try:
driver.quit()
except Exception: # noqa: BLE001 — the browser is gone; nothing left to close
pass
def _dump_path(out_path: str) -> str:
stem = Path(out_path).with_suffix("")
return f"{stem}_debug.html"
def _cdp_endpoint_has_credentials(cdp_endpoint: str) -> bool:
parts = urlparse(cdp_endpoint)
return bool(parts.username or parts.password)
def _build_driver(*, headless: bool, proxy: Optional[Proxy], cdp_endpoint: Optional[str], user_agent: Optional[str] = None):
options = Options()
if headless:
options.add_argument("--headless=new")
options.add_argument("--no-sandbox")
options.add_argument("--disable-dev-shm-usage")
if _CHROME_BINARY:
options.binary_location = _CHROME_BINARY
if user_agent:
options.add_argument(f"--user-agent={user_agent}")
if cdp_endpoint:
options.debugger_address = urlparse(cdp_endpoint).netloc.split("@")[-1]
return webdriver.Chrome(options=options)
if proxy is not None:
if proxy.has_auth:
log.warning(
"Selenium's --proxy-server cannot authenticate — using %s:%s "
"with credentials STRIPPED, not silently dropped.",
proxy.host, proxy.port,
)
options.add_argument(f"--proxy-server={proxy.server_only()}")
if Service and _CHROMEDRIVER_PATH:
service = Service(executable_path=_CHROMEDRIVER_PATH)
elif Service:
service = Service() # Selenium Manager: resolves/downloads over the network
else:
service = None
return webdriver.Chrome(service=service, options=options) if service else webdriver.Chrome(options=options)
# See stockx-scraper's / skyscanner-scraper's selenium_scraper.py for why
# this reads the Navigation Timing API instead of a driver-native Response
# object.
_STATUS_JS = (
"try { return performance.getEntriesByType('navigation')[0].responseStatus || 0; } "
"catch (e) { return 0; }"
)
def _maybe_solve_captcha(
*, html: str, url: str, client: Optional[TwoCaptchaClient], policy: str, budget: SolveBudget,
min_score: float = 0.3,
) -> Optional[dict]:
return rp.captcha_step(html=html, url=url, client=client, policy=policy, min_score=min_score,
budget=budget, logger=log)
def scrape_search(
*, args: argparse.Namespace, start_url: str,
proxy_pool: Optional[ProxyPool], client: Optional[TwoCaptchaClient],
user_agent: Optional[str] = None, store: Optional[Store] = None,
) -> Tuple[List[Product], bool, bool, int, bool]:
"""Returns (products, blocked, remote_api_error, scroll_rounds_done,
shortfall, stop_reason) — see playwright_scraper.scrape_search."""
blocked = False
remote_api_error = False
scroll_error = False
# One paid-solve allowance for this page, shared by every scroll round.
budget = SolveBudget()
proxy = proxy_pool.next() if proxy_pool else None
log.info("Using proxy %s", proxy.masked() if proxy else "(no local proxy pool — direct connection, or a --cdp-endpoint session providing its own exit)")
driver = _build_driver(headless=args.headless, proxy=proxy, cdp_endpoint=args.cdp_endpoint, user_agent=user_agent)
if store is not None:
# CDP sets a cookie for a domain the browser has not visited yet;
# driver.add_cookie() would need a throwaway lidl.com page first.
for name, value in store.cookies():
driver.execute_cdp_cmd("Network.setCookie", {
"name": name, "value": value, "domain": "www.lidl.com", "path": "/", "secure": True,
})
last_error = None
status = None
for attempt in range(args.retries + 1):
try:
driver.set_page_load_timeout(NAV_TIMEOUT_S)
driver.get(start_url)
time.sleep(READINESS_WAIT_S)
try:
reported = driver.execute_script(_STATUS_JS)
status = int(reported) if reported else None
except WebDriverException:
pass
if proxy_pool is not None and proxy is not None:
proxy_pool.report_success(proxy)
last_error = None
break
except WebDriverException as exc:
message = str(exc)
last_error = message
dead = is_proxy_dead_error(message)
if proxy_pool is not None and proxy is not None and dead:
proxy_pool.report_failure(proxy, dead=True)
log.warning("Proxy reported dead: %s", message)
else:
log.warning("Navigation attempt %d/%d failed: %s", attempt + 1, args.retries + 1, message)
if attempt < args.retries:
time.sleep(args.retry_delay)
if last_error is not None:
driver.quit()
log.error("Search page permanently failed to load: %s", last_error)
return [], False, True, 0, False, rp.STOP_NAVIGATION_FAILED
first_html = driver.page_source if status is not None and status >= 400 else ""
if rp.status_means_blocked(status, first_html):
log.warning("Search page returned HTTP %d — treating as blocked, not empty.", status)
blocked = True
seen_skus: set = set()
merged: List[Product] = []
stall = 0
previous_height: Optional[int] = None
clicked_last_round = False
rounds = 0
loop_reason = rp.STOP_MAX_SCROLLS
# A browser that closes mid-run is a transport failure: the rows
# gathered so far are kept (exit 6) instead of a crash (exit 1).
loop_done = False
try:
for round_num in range(args.max_scrolls + 1):
rounds = round_num
html = driver.page_source
captcha_detected = detect_from_html(html, lp.BOT_CHALLENGE_MARKERS)
cards_present = lp.count_result_cards(html) > 0
if captcha_detected and not cards_present:
blocked = True
captcha_result = _maybe_solve_captcha(
html=html, url=start_url, client=client, policy=args.solve_captcha,
budget=budget, min_score=args.min_score,
)
if captcha_result and captcha_result.get("action") in rp.UNSOLVED_ACTIONS:
if lp.count_result_cards(html) == 0:
blocked = True
result = lp.safe_parse_search_results(
html, zip_code=args.zip_code, store_id=args.store_id, max_results=args.max_results,
)
round_skus = {_sku_key(p) for p in result.products}
new_skus = round_skus - seen_skus
page_height = int(driver.execute_script("return document.body.scrollHeight") or 0)
if new_skus:
for p in result.products:
if _sku_key(p) in new_skus:
merged.append(p)
seen_skus |= new_skus
stall = 0
else:
if previous_height == page_height:
stall += 1
else:
stall = 0
previous_height = page_height
if result.products:
blocked = False
if len(merged) >= args.max_results:
merged = merged[: args.max_results]
loop_reason = rp.STOP_MAX_RESULTS
break
if round_num >= args.max_scrolls:
loop_reason = rp.STOP_MAX_SCROLLS
break
try:
load_more = driver.find_elements("css selector", "button.s-load-more__button")
can_load_more = bool(load_more and load_more[0].is_displayed() and load_more[0].is_enabled())
if not new_skus and stall > 0 and not clicked_last_round and can_load_more:
driver.execute_script("arguments[0].click();", load_more[0])
clicked_last_round = True
stall = 0
else:
if stall >= args.stall_rounds:
loop_reason = rp.STOP_STALLED
break
driver.execute_script(
"window.scrollBy(0, Math.max(Math.floor(window.innerHeight * 0.8), 600));"
)
clicked_last_round = False
except WebDriverException as exc:
if rp.browser_gone(exc):
raise # handled once, below the loop
log.warning("Scroll failed, stopping pagination early: %s", exc)
scroll_error = True
break
time.sleep(args.scroll_delay)
loop_done = True
final_html = driver.page_source
except Exception as exc: # noqa: BLE001 — only a closed browser is handled here
if not rp.browser_gone(exc):
raise
rp.log_browser_closed(exc, round_num=rounds, collected=len(merged), logger=log)
_quit_quietly(driver)
# Every round done and only the closing re-read lost: no failed round.
return (merged[: args.max_results], blocked, True, rounds if loop_done else rounds - 1,
not loop_done, rp.STOP_BROWSER_CLOSED)
stop_reason, shortfall, _ = rp.end_of_run(
final_html=final_html, collected=len(merged), max_results=args.max_results,
blocked=blocked, loop_reason=loop_reason, scroll_failed=scroll_error,
)
rp.warn_if_nothing_found(collected=len(merged), blocked=blocked, stop_reason=stop_reason, logger=log)
if args.dump_html:
Path(_dump_path(args.out)).write_text(final_html, encoding="utf-8")
driver.quit()
return merged, blocked, remote_api_error, rounds, shortfall, stop_reason
def run(args: argparse.Namespace) -> int:
started_at = time.time()
try:
start_url = _resolve_start_url(args)
except rp.UsageError as exc:
print(f"Error: {exc}", file=sys.stderr)
return EXIT_BAD_USAGE
if not start_url:
print("Error: provide --url, --query, or --category", file=sys.stderr)
return EXIT_BAD_USAGE
if args.format not in ("json", "csv"):
print(f"Error: unsupported --format {args.format!r}", file=sys.stderr)
return EXIT_BAD_USAGE
if args.cdp_endpoint and _cdp_endpoint_has_credentials(args.cdp_endpoint):
print(
"Error: --cdp-endpoint carries credentials — Selenium/chromedriver's "
"debuggerAddress takes a bare host:port and cannot authenticate a "
"remote session. Use playwright_scraper.py or puppeteer_scraper.py "
"for the Scraping Browser API.", file=sys.stderr,
)
return EXIT_BAD_USAGE
if webdriver is None:
print(f"Error: selenium is not installed ({_SELENIUM_IMPORT_ERROR}). "
f"pip install -r requirements-selenium.txt", file=sys.stderr)
return EXIT_CRASH
args.out = args.out or _default_out(args.format)
rp.apply_scroll_default(args)
try:
store = rp.resolve_store(args)
except rp.UsageError as exc:
print(f"Error: {exc}", file=sys.stderr)
return EXIT_BAD_USAGE
except StoreApiError as exc:
log.error("Could not look up the Lidl store to price against: %s", exc)
return EXIT_REMOTE_API_ERROR
try:
proxies = load_proxies(args.proxy, args.proxy_file)
except ProxyParseError as exc:
print(f"Error: {exc}", file=sys.stderr)
return EXIT_BAD_USAGE
proxy_pool = ProxyPool(proxies, shuffle=args.proxy_shuffle, block_retries=args.proxy_block_retries) if proxies else None
if args.cdp_endpoint and proxy_pool is not None:
log.warning("Ignoring --proxy: a --cdp-endpoint session already carries its own exit IP.")
proxy_pool = None
client = None
if args.twocaptcha_key and (args.solve_captcha != "off" or args.fingerprint):
client = TwoCaptchaClient(args.twocaptcha_key, api_base=args.captcha_api)
user_agent = None
cdp_refused_fingerprint = args.fingerprint and refuse_if_cdp(args.cdp_endpoint)
if args.fingerprint and not cdp_refused_fingerprint:
if client is None:
log.warning("--fingerprint requested but no --twocaptcha-key/TWOCAPTCHA_KEY set — continuing without one.")
else:
profile = fetch_fingerprint(client, tags=args.fp_tags, country=args.fp_country)
if profile:
user_agent = user_agent_from(profile)
try:
merged, blocked, remote_api_error, rounds, scroll_error, stop_reason = scrape_search(
args=args, start_url=start_url, proxy_pool=proxy_pool, client=client, user_agent=user_agent,
store=store,
)
price_confirmed_pct = (sum(1 for p in merged if p.price is not None) / len(merged)) if merged else None
except Exception:
log.exception("Unhandled error — this is a crash, not a normal blocked/empty run")
return EXIT_CRASH
completed_rounds = rounds + 1
failed_pages = [completed_rounds + 1] if scroll_error else None
return finish_run(
products=merged, out_path=args.out, fmt=args.format, engine=ENGINE_NAME, url=start_url,
pages_requested=args.max_scrolls, pages_completed=completed_rounds, failed_pages=failed_pages,
blocked=blocked, remote_api_error=remote_api_error, allow_empty=args.allow_empty,
started_at=started_at, price_confirmed_pct=price_confirmed_pct,
parser_failed=stop_reason == rp.STOP_PARSER_FOUND_NOTHING, stop_reason=stop_reason,
)
def main() -> int:
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s")
args = build_arg_parser().parse_args()
args = env_config.apply_env(args)
try:
return run(args)
except KeyboardInterrupt:
return EXIT_CRASH
if __name__ == "__main__":
sys.exit(main())