Repository navigation
Expand file tree
/
Copy pathselenium_scraper.py
More file actions
1296 lines (1177 loc) · 65 KB
/
Copy pathselenium_scraper.py
File metadata and controls
1296 lines (1177 loc) · 65 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
#!/usr/bin/env python3
"""
craigslist-scraper -- Selenium edition (secondary engine)
=========================================================
The same scrape as playwright_scraper.py, driven through Selenium. It must
agree with its twins on exit codes, run status, and whether a run crashes or
spends money -- the decisions that determine all three live in page_flow.py
and output_writer.finish_run(), so this file is browser plumbing and nothing
else.
--mode listing (default) a result list
--mode posting one /view/d/{slug}/{token} advert
Three limits of this engine, stated here rather than left to be discovered.
None is a bug in this code and none can be fixed from here:
* **Selenium cannot use an authenticated remote CDP endpoint.** Playwright's
`connect_over_cdp` and pyppeteer's `browserWSEndpoint` take a full
`ws://user:pass@host:port` and authenticate on the WebSocket upgrade.
chromedriver's `debuggerAddress` takes a bare `host:port` and has nowhere
to put a password. So --cdp-endpoint here works only for an endpoint that
needs no credentials; a credentialed one is refused with exit 2 rather
than connected to and silently failing.
* **Selenium cannot authenticate a proxy at all.** `--proxy-server=` accepts
no credentials, and there is no equivalent of pyppeteer's
`page.authenticate`. Credentials are stripped and a warning says so, so
nobody believes a `user:pass` URL is doing something.
* **JavaScript cannot be switched off per run here.** The Playwright engine
disables it for a single-batch listing run, which is what stops
Craigslist's application removing the served result list before the
snapshot is taken. Chrome's equivalent is a profile preference set at
launch, and this engine sets it -- but a remote `debuggerAddress` browser
brings its own profile, so over --cdp-endpoint the setting does not
apply. The parser falls back to the rendered grid in that case, which
costs the structured columns rather than the run.
There is no --concurrency here either, and on this site there is none
anywhere: no batch of a Craigslist listing has an address of its own.
Usage
-----
python selenium_scraper.py \\
--url "https://www.craigslist.org/search/area/newyork?cat=sss" --pages 3
Requires: pip install -r requirements.txt -r requirements-selenium.txt
Selenium 4 fetches a matching chromedriver itself; a local Chrome
or Chromium must be installed.
"""
import argparse
import logging
import re
import sys
import time
from dataclasses import dataclass, field
from typing import List, Optional
from urllib.parse import urlparse, urlsplit, parse_qsl
from selenium import webdriver
from selenium.common.exceptions import (TimeoutException, WebDriverException,
JavascriptException)
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from captcha_solver import (detect_recaptcha_v3, detect_recaptcha_in_page,
reconcile_detections, solve_recaptcha,
CaptchaUnsolvable, INJECT_TOKEN_JS)
from product_parser import (parse_products, parse_posting,
parse_rendered_cards, page_currency, SELECTORS,
detect_bot_challenge, listing_kind, site_host,
is_supported_host, total_results, search_header,
unsupported_reason, served_by_craigslist,
RESULT_CAP)
from output_writer import dedupe_by_key, finish_run, EXIT_API_ERROR
import page_flow
from page_flow import MIN_CARD_MATCHES
from proxy_pool import (from_args as proxy_pool_from_args, mask, ROTATE_MODES,
ProxyError, split_credentials)
import env_config
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s")
logger = logging.getLogger("selenium_scraper")
ITEM_LINK_SELECTOR = page_flow.READY_SELECTOR_LISTING
# The lowest PRICE coverage that is still healthy, per page kind. Not a
# share of rows that got a price at all. Measured across 14 captures: 95%-100%
# on for-sale-type categories, 100% on housing, jobs, services and community.
# ENRICHMENT_FLOOR is the structured-data share, applied only to the rows that
# could carry it -- 66% (paris) to 99% (toronto) where the page publishes any,
# and a measured zero where it publishes none.
PRICE_FLOOR = 90
ENRICHMENT_FLOOR = 60
# A page holding less than this share of the fullest page in the same run is
# reported as thin. There is no fixed batch size here -- 41 entries on the
# smallest served list measured and 359 on the largest -- and the LAST batch
# is legitimately short, so the bar stays loose.
THIN_PAGE_SHARE = 0.5
PAGE_LOAD_TIMEOUT = 60
SCRIPT_TIMEOUT = 30
# Chromium's own names for "the proxy is the problem, not the site". A dead
# proxy and a slow page want opposite responses — a different exit versus
# another try at the same one — so they are told apart by the error text.
_PROXY_ERROR_MARKERS = (
"ERR_PROXY_CONNECTION_FAILED", "ERR_TUNNEL_CONNECTION_FAILED",
"ERR_PROXY_AUTH_UNSUPPORTED", "ERR_PROXY_AUTH_REQUESTED",
"ERR_UNEXPECTED_PROXY_AUTH", "ERR_PROXY_CERTIFICATE_INVALID",
)
@dataclass
class PageOutcome:
"""What one page produced. Mirrors playwright_scraper.PageOutcome."""
page_num: int
url: str
final_url: Optional[str] = None
products: List = field(default_factory=list)
blocked_by: Optional[str] = None
load_failed: bool = False
state: Optional[str] = None
# Present only on a run that let the application paint: the served markup
# states no total at all. Kept so
# the sidecar's shape matches the family's.
total_available: Optional[int] = None
# The raw result-count header, verbatim, for the sidecar.
header: Optional[str] = None
# What the walk through the virtualised list did: how far the site's own
# counter got, how many steps it took, how many GAPS it opened, and
# whether it hit the 10,000 ceiling. A walk with gaps is PARTIAL -- the
# window moved past rows nobody read.
walk: Optional[dict] = None
@property
def ok(self) -> bool:
return not self.load_failed and self.blocked_by is None
# Every `scheme://user:pass@` in a string, however many times it occurs.
# Matching globally rather than once is the point: a driver's connection
# error can repeat the endpoint several times (the message plus a call log),
# so a masker that handled only the first occurrence would print the password
# the other times and look like it was working.
_CREDENTIALS_IN_URL_RE = re.compile(r"([a-z][a-z0-9+.\-]*://)[^\s/@]+:[^\s/@]+@",
re.IGNORECASE)
def _mask_credentials(text: str) -> str:
"""`text` with any username:password in an embedded URL replaced.
Takes arbitrary text, not just a URL, because the strings that most need
this are exception messages with a URL inside them. The host and port are
KEPT — which endpoint or exit a run used is the useful half of the line
and is not the secret.
"""
return _CREDENTIALS_IN_URL_RE.sub(r"\1***:***@", text or "")
def _chrome_ua(version: str) -> str:
"""A desktop-Chrome UA naming the browser's OWN real version.
`driver.capabilities["browserVersion"]` is the installed Chrome's version,
so the claim matches what the JS engine and the TLS handshake report. A
hardcoded number drifts the moment Chrome updates, and claiming an older
Chrome than everything else reports is itself a signal.
"""
return (f"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
f"(KHTML, like Gecko) Chrome/{version} Safari/537.36")
def _cdp_host_port(endpoint: str) -> str:
"""`host:port` for chromedriver's debuggerAddress, or exit 2 with a reason.
chromedriver takes a bare address here and cannot send credentials, so an
endpoint that carries them cannot work through this engine. Refused up
front: connecting anyway would fail somewhere further in with an error
that names none of this.
"""
parts = urlsplit(endpoint if "//" in endpoint else f"//{endpoint}")
if parts.username or parts.password:
logger.error(
"This --cdp-endpoint carries credentials (%s), and Selenium cannot "
"send them: chromedriver's debuggerAddress is a bare host:port. "
"Use playwright_scraper.py or puppeteer_scraper.py for a "
"credentialed endpoint such as the Scraping Browser API — both "
"authenticate on the WebSocket upgrade.",
_mask_credentials(endpoint))
sys.exit(2)
host = parts.hostname or endpoint
port = f":{parts.port}" if parts.port else ""
return f"{host}{port}"
class _Session:
"""One Chrome driver, relaunchable onto a different exit.
Same contract as the Playwright engine's _BrowserSession, including the
rule that a rotation means a genuinely FRESH browser — and a
fresh browser is also the only thing that re-rolls the served page
fresh cookie jar is what an ordinary user on another network looks like.
"""
def __init__(self, args, pool):
self.args, self.pool = args, pool
self.remote = bool(args.cdp_endpoint)
self.driver = None
# Whether this session runs the site's own application. Only walking
# past the first batch does; see `open` below, and page_flow's "So
# JavaScript is DISABLED for a single-batch run".
#
# NOT applied over --cdp-endpoint: that attaches to a browser someone
# else launched, whose profile is already set. The parser falls back
# to the rendered grid there, which costs the structured columns
# rather than the run.
self.javascript = (args.mode == "listing"
and page_flow.javascript_needed(args.pages))
def open(self):
options = Options()
# Stop waiting at DOMContentLoaded rather than at `load`, which is
# what Playwright's `wait_until="domcontentloaded"` does -- so the
# three engines snapshot the page at the same MOMENT and not just
# with the same code.
#
# It matters on this site rather than being a tidiness point. The
# served result list is removed by the application about twenty
# seconds in; waiting for `load` on a page with hundreds of images can
# spend enough of that window to miss it, and the run then falls back
# to the rendered grid and silently loses the structured columns.
# Measured: a Selenium walk that snapshotted at `load` got 200 rows
# from the grid where the Playwright walk got 353 from the served
# list.
options.page_load_strategy = "eager"
if self.remote:
options.debugger_address = _cdp_host_port(self.args.cdp_endpoint)
logger.info("Attaching to an existing browser at %s.",
options.debugger_address)
# No UA, no proxy, no fingerprint on this path: the remote browser
# brings its own, and stacking a second creates a contradiction
# rather than better cover.
self.driver = webdriver.Chrome(options=options)
self._apply_timeouts()
return self
if self.args.headless:
options.add_argument("--headless=new")
options.add_argument("--no-sandbox")
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--window-size=1600,1000")
# Not a fingerprint measure, a correctness one: without it Chrome
# advertises "HeadlessChrome", which is a giveaway on any site with
# a bot manager in front of it.
options.add_argument("--disable-blink-features=AutomationControlled")
if not self.javascript:
# A single-batch listing run does not run the site's application,
# and on this site that is what KEEPS the data: Craigslist serves
# a complete result list and its own script removes it about
# twenty seconds later, replacing it with a virtualised grid of
# 200 recycled nodes. With JavaScript off the list stays, the
# hydration wait is not paid, and the window in which the page has
# neither view cannot be hit.
#
# Chrome's switch for this is a content-setting preference, not a
# command-line flag: `--disable-javascript` was removed years ago
# and passing it does nothing at all, silently.
options.add_experimental_option(
"prefs", {"profile.managed_default_content_settings.javascript": 2})
logger.info("JavaScript is off for this run: one batch comes from "
"the served result list, which the site's own script "
"would otherwise remove before it can be read.")
if self.pool:
scrubbed, credentials = split_credentials(self.pool.current)
options.add_argument(f"--proxy-server={scrubbed}")
logger.info("Using proxy exit %s", mask(self.pool.current))
if credentials:
logger.warning(
"This proxy has credentials and SELENIUM CANNOT SEND "
"THEM: --proxy-server accepts an address only, and there "
"is no Selenium equivalent of pyppeteer's "
"page.authenticate. They have been stripped, so requests "
"will go out unauthenticated and the exit will most "
"likely refuse them. Use playwright_scraper.py or "
"puppeteer_scraper.py for an authenticated proxy.")
self.driver = webdriver.Chrome(options=options)
self._apply_timeouts()
version = self.driver.capabilities.get("browserVersion", "")
if version:
# Set over CDP rather than as a launch switch, so it can use the
# version the driver actually reports.
try:
self.driver.execute_cdp_cmd(
"Network.setUserAgentOverride",
{"userAgent": _chrome_ua(version)})
except WebDriverException as e:
logger.debug("Could not override the user agent: %s", e)
if self.args.fingerprint:
self._apply_fingerprint()
return self
def _apply_timeouts(self):
# Explicit, because a driver that stops answering otherwise hangs the
# run: "every remote call is bounded" applies to this engine too.
self.driver.set_page_load_timeout(PAGE_LOAD_TIMEOUT)
self.driver.set_script_timeout(SCRIPT_TIMEOUT)
def _apply_fingerprint(self):
from fingerprint_client import (get_fingerprint, playwright_init_script,
fingerprint_user_agent)
fp = get_fingerprint(self.args.twocaptcha_key, tags=self.args.fp_tags,
country=self.args.fp_country)
# Through the shared helper, NOT by reaching into the response.
#
# This line read `fp["userAgent"]["value"]` -- a key the API returns
# in neither of its two formats -- so `--fingerprint` quietly set no
# user agent here at all while reporting that it had applied one. The
# API's own key is `userAgent.userAgent`, and the helper is where that
# knowledge lives precisely so three engines cannot each get it wrong
# separately. Verified against a live response, 2026-09-14.
ua = fingerprint_user_agent(fp)
intl = fp.get("intl") or {}
script = playwright_init_script(fp)
try:
if ua:
self.driver.execute_cdp_cmd("Network.setUserAgentOverride",
{"userAgent": ua})
else:
logger.warning("This fingerprint carries no user agent, so the "
"browser keeps its own. That is a contradiction "
"worth knowing about rather than a silent "
"half-application.")
# The timezone the API states, which was not being applied at all.
# A fingerprint whose locale says America/New_York over a browser
# reporting Europe/Berlin is a mismatch of exactly the kind a
# fingerprint exists to avoid.
if intl.get("timeZone"):
self.driver.execute_cdp_cmd("Emulation.setTimezoneOverride",
{"timezoneId": intl["timeZone"]})
# The same patch script the Playwright engine installs on its
# context. Shared deliberately: two engines applying different
# halves of one fingerprint would be a contradiction of exactly
# the kind a fingerprint is meant to avoid.
self.driver.execute_cdp_cmd(
"Page.addScriptToEvaluateOnNewDocument", {"source": script})
logger.info("Using 2captcha fingerprint %s (%s), UA %s, timezone %s",
fp.get("id"), fp.get("country"),
"set" if ua else "NOT set", intl.get("timeZone") or "not stated")
except WebDriverException as e:
logger.warning("Could not apply the fingerprint over CDP (%s) -- "
"continuing without it.", e)
def relaunch(self):
if self.remote:
return
self.close()
self.open()
def close(self):
try:
if self.driver is not None:
# quit(), not close(): close() ends one window and leaves the
# driver process running, which on a per-page rotation would
# leak a chromedriver per page.
self.driver.quit()
except Exception as e: # noqa: BLE001 — teardown must not mask the reason we're here
logger.debug("Ignoring error during driver teardown: %s", e)
# ---------------------------------------------------------------------------
# page_flow, bound to Selenium
# ---------------------------------------------------------------------------
# Only "how to ask this driver" lives here. Note the JS dialect: Selenium's
# execute_script runs a function BODY and needs an explicit `return`, unlike
# the `() => expr` both other engines take — which is why page_flow names
# operations instead of passing JavaScript.
def _driver(session):
driver = session.driver
def count(selector):
try:
return len(driver.find_elements(By.CSS_SELECTOR, selector))
except WebDriverException as e:
logger.debug("count(%s) failed: %s", selector, e)
return 0
def sleep(ms):
time.sleep(ms / 1000.0)
def content():
try:
return driver.page_source
except WebDriverException as e:
# A geo-redirect or the consent layer can navigate, so a
# snapshot can land on the document swap. None tells the caller to
# skip a check rather than fail the run.
logger.debug("page_source unavailable (page navigating?): %s", e)
return None
def current_url():
try:
return driver.current_url
except WebDriverException:
return ""
def scroll_by(px):
# Selenium's execute_script takes a function BODY with an explicit
# `return` -- not `() => expr`, which is what the other two drivers
# take. That disagreement is exactly why page_flow names the
# OPERATION and each engine spells it in its own dialect.
#
# A BOUNDED wheel, deliberately not a jump to the document's bottom.
# The family's rule is the opposite and it is right for a lazy-loading
# grid that grows as it loads. Craigslist's list is virtualised: its
# container's height is pre-computed for all 10,000 items, so one jump
# to scrollHeight lands on item 9,997 and collects 7% of the list
# while passing every readiness test the usual rule prescribes.
try:
driver.execute_script("window.scrollBy(0, arguments[0]);", px)
except WebDriverException:
pass
def text(selector):
# The first match's text, or None -- used for the site's own
# "1 - 6 of 10,000+" counter, which is what the walk's stall check
# reads instead of DOM churn.
try:
els = driver.find_elements(By.CSS_SELECTOR, selector)
return els[0].text if els else None
except WebDriverException:
return None
return {"count": count, "sleep": sleep, "content": content,
"current_url": current_url, "scroll_by": scroll_by, "text": text}
def _parse_for_mode(html: str, url: str, args, page_num: int = 1) -> List:
# `page_num` is threaded through rather than defaulted: `position`
# restarts at 1 on every page, so without the page number beside it a
# row from page 2 claims the same position as one from page 1. Mirrors
# playwright_scraper._parse_for_mode exactly.
if args.mode == "posting":
row = parse_posting(html, url, category=args.category)
return [row] if row is not None else []
return parse_products(html, url, page=page_num, category=args.category)
def _next_page_candidates(session, page_num: int) -> List[str]:
"""The site's own next-page link, resolved by the browser, or None.
Returns EVERY candidate, filtered by page_flow. On Craigslist that filter
returns nothing and the selector is empty, because no page measured
carries a next-page link of any kind -- so this exists to NOTICE if that
ever changes, not to be relied on.
Reads the DOM's `.href` property, which is already absolute — the
opposite of Playwright's get_attribute("href"), which returns the raw
attribute. Kept explicit because the engines differ here.
Note the JS is a function BODY with an explicit `return`, not the arrow
expression the other two engines pass. That difference is exactly why no
JavaScript crosses the page_flow boundary.
"""
selector = page_flow.next_page_selector(page_num)
if not selector:
# Empty on this site, and passing "" to querySelectorAll raises a
# SyntaxError in the browser. Returning early rather than letting the
# exception handler below swallow it: normal flow should not travel
# through an except branch.
return []
try:
hrefs = session.driver.execute_script(
"return Array.from(document.querySelectorAll(arguments[0]))"
".map(a => a.href || a.getAttribute('href')).filter(Boolean);",
selector)
except WebDriverException:
return []
return page_flow.next_page_candidates(session.driver.current_url,
hrefs or [])
def handle_captcha_if_present(session, args, *, budget) -> bool:
"""Detect and solve a challenge. True if something was solved.
Same detectors, same reconciliation and the same "detected is not
blocking" rule as the Playwright engine — the three must agree about
when a run spends money.
NOTE what this cannot help with. No challenge of any kind has been
observed on this site across 22 captures, and what Craigslist serves a
refused address has not been measured here at all. A refusal with no
widget on it has nothing to solve, so no solve applies there and none is
attempted. See product_parser.detect_page_state.
"""
driver = session.driver
d = _driver(session)
html = d["content"]()
if html is None:
return False
selector = page_flow.ready_selector(args.mode)
already_rendered = d["count"](selector)
when_blocked = getattr(args, "solve_captcha", "when-blocked") == "when-blocked"
html_challenge = detect_recaptcha_v3(html, d["current_url"]())
runtime_challenge = detect_recaptcha_in_page(
lambda js: driver.execute_script(f"return ({js})();"),
page_url=d["current_url"]())
challenge = reconcile_detections(html_challenge, runtime_challenge)
if not challenge:
return False
if when_blocked and already_rendered > MIN_CARD_MATCHES:
logger.info("%s detected via %s, but %d anchors are already on the "
"page — not solving it.", challenge.kind, challenge.source,
already_rendered)
return False
logger.warning("%s detected via %s (sitekey=%s) — attempting to solve.",
challenge.kind, challenge.source, challenge.sitekey)
if not args.twocaptcha_key:
logger.warning("No 2captcha API key, so this challenge cannot be solved.")
return False
if not budget.spend():
# Checked BEFORE the purchase: the allowance for this page is spent,
# whichever call site (before or after classification) used it.
logger.warning("Captcha budget for this page is spent (%d of %d) — "
"not buying another solve.", budget.spent, budget.limit)
return False
try:
token = solve_recaptcha(challenge, args.twocaptcha_key,
api_version=args.captcha_api,
min_score=args.min_score)
except Exception as e: # noqa: BLE001
logger.error("Solving the challenge failed (%s).", e)
return False
try:
driver.execute_script(f"return ({INJECT_TOKEN_JS})(arguments[0]);", token)
except WebDriverException as e:
logger.error("Could not inject the token (%s).", e)
return False
logger.info("Token injected. Reloading page to continue.")
time.sleep(1.5)
driver.refresh()
return True
def _fetch_one_page(session, args, pool, page_num: int, url: str) -> PageOutcome:
"""Fetch and parse one page. Mirrors playwright_scraper._fetch_one_page.
Kept structurally parallel to its twins on purpose — "all three engines
agree" is checked by reading them side by side as well as by the smoke
suite.
"""
outcome = PageOutcome(page_num=page_num, url=url)
d = _driver(session)
html, state, load_failed = None, "ok", False
# See the Playwright engine for the measurement: without a pool there is
# no exit to rotate to, but a plain re-fetch is what clears a block on a
# Scraping Browser profile, so the budget is not zero.
has_pool = bool(pool and len(pool) > 1)
# `RETRY_ON_BLOCKED` is CONSULTED, not just documented. It was a
# constant with a paragraph of justification that no engine read — a
# policy statement nothing enforced, which is the same defect as dead
# code that looks load-bearing. Setting it False now really does stop
# the retry loop.
block_retries = 0 if not page_flow.RETRY_ON_BLOCKED else (
args.proxy_block_retries if has_pool
else page_flow.BLOCK_RETRIES_WITHOUT_POOL)
# ONE allowance for the whole block-retry loop, shared by BOTH captcha
# call sites below and asked before any purchase: a page that keeps
# coming back as a challenge would otherwise buy one solve per attempt
# and per rotation, which is how a run quietly turns into a bill. See
# page_flow.SolveBudget.
budget = page_flow.SolveBudget()
for block_attempt in range(block_retries + 1):
logger.info("Fetching page %d/%d: %s", page_num, args.pages, url)
load_failed, exit_failed = False, None
for attempt in range(1, args.retries + 1):
try:
session.driver.get(url)
load_failed = False
break
except (TimeoutException, WebDriverException) as e:
text = str(e)
reason = next((m for m in _PROXY_ERROR_MARKERS if m in text), "")
load_failed = True
if reason:
exit_failed = reason
break # a different exit is the only thing that helps
if attempt < args.retries:
pause = args.retry_delay * (2 ** (attempt - 1))
logger.warning("Failed to load %s (attempt %d/%d: %s) — "
"retrying in %.1fs.", url, attempt,
args.retries, text[:120], pause)
time.sleep(pause)
if exit_failed and has_pool and block_attempt < block_retries:
logger.warning("Exit %s is unusable (%s) — rotating to another "
"one (%d/%d).", mask(pool.current), exit_failed,
block_attempt + 1, block_retries)
pool.advance(f"unusable exit: {exit_failed}")
session.relaunch()
d = _driver(session)
continue
if load_failed:
break
if handle_captcha_if_present(session, args, budget=budget):
time.sleep(1)
html = d["content"]() or ""
state = page_flow.classify(html, url=d["current_url"]())
# "Not painted yet" is not a fault. A REAL state on this site rather
# than a theoretical one: the application removes the served result
# list about twenty seconds in and paints its own grid several
# seconds later, so there is a window in which the page has neither.
# Waiting is the right answer there -- retrying would throw away a
# page that is about to be fine. Mirrors
# playwright_scraper exactly; see page_flow.should_wait.
if page_flow.should_wait(state):
js = args.mode == "listing" and page_flow.javascript_needed(args.pages)
wait_s = page_flow.content_timeout_ms(args.mode, js=js) / 1000.0
sel = page_flow.ready_selector(args.mode)
need = page_flow.min_matches(args.mode)
logger.info("Batch %d is a page Craigslist served whose results "
"are not in it yet (%d bytes) -- waiting up to %.0fs "
"rather than spending a retry on it.",
page_num, len(html), wait_s)
found = page_flow.wait_for_count(d["count"], d["sleep"], sel,
need, int(wait_s * 1000))
if found < need:
logger.info("Still nothing after %.0fs (%d match(es)).",
wait_s, found)
html = d["content"]() or html
state = page_flow.classify(html, url=d["current_url"]())
# No interstitial-settling step, and its absence is measured rather
# than an omission: 22 captures covering six categories and five
# locales carry no interstitial of any kind, and the word "captcha"
# appears zero times on any of them. There is nothing to wait out.
#
# The paid path is reached only for state "challenge", which no
# capture of this site has ever produced. Wired up because a bot
# manager can be switched on between deploys, and bounded by
# SOLVES_PER_PAGE so a speculative path cannot become a bill.
if page_flow.should_solve(state) and budget.remaining():
if handle_captcha_if_present(session, args, budget=budget):
time.sleep(1)
html = d["content"]() or html
state = page_flow.classify(html, url=d["current_url"]())
if state == "content":
logger.info("The solve was accepted — page %d is content "
"now.", page_num)
else:
logger.warning("The solve was NOT accepted: page %d is "
"still %s. The purchase is spent.",
page_num, state)
if not page_flow.should_retry(state):
# "content" and "empty" are both final answers. An empty page is
# a CORRECT one — an area landing page has no results — so retrying it
# would re-confirm the same right answer, and rotating the exit
# would blame an address for the URL it was given.
break
# Blocked or challenged. The ADDRESS is what was scored, not the URL,
# so a different exit is the only thing that plausibly changes the
# outcome.
if block_attempt < block_retries:
if has_pool:
logger.warning("Page %d came back as %s from %s — retrying "
"from another exit (%d/%d).", page_num, state,
mask(pool.current), block_attempt + 1,
block_retries)
pool.advance(f"{state} on page {page_num}")
session.relaunch()
d = _driver(session)
else:
# No pool, so nowhere else to go but a plain re-fetch through
# the same access path, and no pool to read an exit from:
# this branch used to call `pool.current` on None and end the
# run with exit 1. Mirrors playwright_scraper. The browser is
# not relaunched, for the same reason given there (one live
# connection per Scraping Browser profile).
pause = args.retry_delay * (block_attempt + 1)
logger.warning("Page %d came back as %s — re-fetching through "
"the same access path in %.1fs (%d/%d).",
page_num, state, pause, block_attempt + 1,
block_retries)
time.sleep(pause)
if load_failed:
logger.error("Gave up loading %s after %d attempt(s).", url, args.retries)
outcome.load_failed = True
return outcome
outcome.state = state
if state == "blocked":
# There is no challenge on this site to solve. Craigslist sends an
# address it has scored NOTHING — no status code, no interstitial, no
# vendor marker — so a 2Captcha key does not help and a residential
# exit does. The dump is written even when empty: "0 bytes" is itself
# the diagnosis here.
debug_html = f"{args.out}_page{page_num}_debug.html"
with open(debug_html, "w", encoding="utf-8") as f:
f.write(html or "")
logger.error(
"This response was not a Craigslist page -- %d bytes, %s the "
"site's own asset host, saved to %s. The check is structural "
"rather than a marker list, which is what answers correctly for "
"an interstitial, a block page and Chromium's own network-error "
"page alike -- that last one carries the site's hostname in its "
"<title> and would fool any title test. No challenge vendor has "
"ever been observed here, so a key is unlikely to be the answer; "
"a different exit is the thing to try. Note this engine cannot "
"use an authenticated remote CDP endpoint or an authenticated "
"proxy; see the README's engine limits. This is exit 3, distinct "
"from a genuinely empty result (exit 4).",
len(html or ""),
"which references" if served_by_craigslist(html or "")
else "with no reference to", debug_html)
outcome.blocked_by = "no-response" if not html else "not-served"
outcome.final_url = d["current_url"]()
return outcome
if state == "unreached":
# Nothing came back: an empty document, which is what a browser holds
# after a navigation that FAILED (a dead or unauthenticated proxy, no
# route). It is not a refusal -- this site has never been seen to
# refuse a request -- so it is not exit 3, which would send the reader
# looking for one. The retries above already spent the block budget.
logger.error(
"Nothing came back for page %d -- %d byte(s), an empty document. "
"That is a navigation that failed (a proxy that is down or "
"rejecting its credentials, or no route to the site), not a "
"refusal: exit 5, the content was never obtained.",
page_num, len(html or ""))
outcome.load_failed = True
outcome.final_url = d["current_url"]()
return outcome
walked_rows = []
walked_seen = set()
if state == "content":
# The served list is already in `html`, captured right after
# navigation. It is the COMPLETE first batch, with the page's own
# JSON-LD aligned onto it, and it is what a single-batch run returns.
#
# Hold a reference before anything else: with JavaScript running, the
# application removes that list about twenty seconds in and paints a
# virtualised grid in its place.
served_html = html
selector = page_flow.ready_selector(args.mode)
threshold = page_flow.min_matches(args.mode)
js = args.mode == "listing" and page_flow.javascript_needed(args.pages)
timeout_s = page_flow.content_timeout_ms(args.mode, js=js) / 1000.0
# A POLL through the shared helper, so all three engines wait the same
# way. This engine could use WebDriverWait, but a shared wait is one
# fewer thing for the three to drift on.
found = page_flow.wait_for_count(d["count"], d["sleep"], selector,
threshold, int(timeout_s * 1000))
if found < threshold:
if args.mode == "posting":
logger.info("The advert's body did not appear within %.0fs. A "
"posting page is server-rendered on every category "
"measured, so this is unusual rather than slow; "
"the parse below decides.", timeout_s)
else:
logger.info("Fewer than %d result entries appeared within "
"%.0fs. If this URL's query matches nothing, that "
"is the expected answer and the run reports 0 rows "
"(exit 4).", threshold, timeout_s)
if args.mode == "listing" and args.pages > 1:
currency = page_currency(served_html)
logger.info("Walking the virtualised list for batches 2-%d. The "
"currency for those rows is read once, here (%s): the "
"page's own JSON-LD is itself virtualised.",
args.pages, currency or "none published")
page_flow.wait_for_count(d["count"], d["sleep"],
SELECTORS["rendered_card"],
page_flow.MIN_CARD_MATCHES,
page_flow.CONTENT_TIMEOUT_MS_HYDRATED)
def _harvest():
"""Read the cards on screen now, and say which ones they were.
The returned set is what lets the walk notice it has outrun
itself: two consecutive reads with no id in common mean the
window jumped a stretch that will never be read, because the
nodes behind it are already recycled.
"""
snapshot = d["content"]()
if not snapshot:
return set()
seen_now = set()
for row in parse_rendered_cards(
snapshot, d["current_url"](), category=args.category,
currency=currency):
if not row.sku:
continue
seen_now.add(row.sku)
if row.sku not in walked_seen:
walked_seen.add(row.sku)
walked_rows.append(row)
return seen_now
outcome.walk = page_flow.walk_virtualised(
count=d["count"], text=d["text"], scroll_by=d["scroll_by"],
sleep=d["sleep"], harvest=_harvest,
want_items=args.pages * page_flow.BATCH_HINT)
outcome.walk["harvested"] = len(walked_rows)
logger.info("The walk reached position %d in %d step(s) and "
"harvested %d distinct row(s).",
outcome.walk["counter_reached"], outcome.walk["steps"],
len(walked_rows))
if outcome.walk["gaps"]:
logger.warning(
"The walk outran its harvest %d time(s), so stretches of "
"the list went by unread and this run is PARTIAL. Its row "
"count is a floor.", outcome.walk["gaps"])
if outcome.walk["counter_reached"] >= RESULT_CAP:
logger.warning(
"This URL hit Craigslist's %d-result ceiling -- the site's "
"limit for ONE address, not the size of the catalogue. "
"Narrow the URL with the site's own filters and run each "
"separately.", RESULT_CAP)
html = served_html
if args.dump_html:
dump_path = (args.dump_html if args.pages == 1
else f"{args.dump_html}.page{page_num}")
with open(dump_path, "w", encoding="utf-8") as f:
f.write(html)
logger.info("Saved the snapshot the parser sees to %s (%d bytes).",
dump_path, len(html))
# Only when the page is NOT already content. A challenge marker on a
# page whose products have rendered guards nothing — and over
# --cdp-endpoint the Scraping Browser's own auto-solve extension injects
# such markers into every page it loads.
# Only for a state page_flow already counts as BLOCKED. An EMPTY page is
# a correct answer, and in a sibling repo (tokopedia-scraper) a served
# hub page reported exit 3 because that site's own performance script
# names `akamaihd.net`. Mirrors playwright_scraper exactly.
vendor = (detect_bot_challenge(html, url=d["current_url"]())
if page_flow.counts_as_blocked(state) else None)
if vendor:
debug_html = f"{args.out}_page{page_num}_debug.html"
with open(debug_html, "w", encoding="utf-8") as f:
f.write(html)
try:
session.driver.save_screenshot(f"{args.out}_page{page_num}_debug.png")
except WebDriverException as e:
logger.warning("Could not capture screenshot: %s", e)
logger.error("Blocked by %s before parsing (%d bytes) — saved to %s. "
"This is exit 3, distinct from a genuinely empty result "
"(exit 4).", vendor, len(html), debug_html)
outcome.blocked_by = vendor
return outcome
final_url = d["current_url"]() or url
# Through the POLICY rather than unconditionally. `STATE_POLICY` is the
# one place that says which states are worth reading, and until now
# nothing consulted its `parse` column: the engines parsed whatever
# reached this line, so every state without an earlier `return` was
# read regardless of what the table said.
#
# # Latent here rather than live — measured on this repo's own fixtures on
# 2026-09-23: no shipped fixture of a parse:False state yields a row,
# because the parser is independently defensive. The gate is wired
# anyway, because "the parser happens to return nothing" is not the
# same guarantee as "the policy says do not read this", and two
# siblings had exactly this shape turn into phantom rows.
#
# Measured across the family on 2026-09-23 by counting definitions
# against readers: 7 of 24 repos defined `should_parse` and none of
# them called it.
products = (_parse_for_mode(html, final_url, args, page_num)
if page_flow.should_parse(state) else [])
if walked_rows:
# Batch 1 came from the served list, complete and enriched. The walk's
# rows come from the rendered grid and carry less -- no coordinates,
# no structured price -- which is what `price_source` records.
#
# Rows already in batch 1 reappear here (the walk starts at the top of
# the same list) and are dropped by the run's dedupe, in page order,
# after the merge. Not here: doing it in two places is how the two
# disagree.
first_skus = {r.sku for r in products if r.sku}
fresh = [r for r in walked_rows if r.sku not in first_skus]
logger.info("The walk added %d row(s) beyond the served list's %d.",
len(fresh), len(products))
for i, row in enumerate(fresh, start=1):
row.page = 2 + (i - 1) // max(len(products), 1)
row.position = 1 + (i - 1) % max(len(products), 1)
products = products + fresh
logger.info("Parsed %d row(s) from batch %d.", len(products), page_num)
if args.mode == "listing" and page_num == 1:
# The served markup publishes NO result total -- the rendered header
# "Menampilkan 1 - 60 barang dari total untuk …" with the total
# EMPTY — so there is no arithmetic completeness check here. The
# header is recorded verbatim instead.
outcome.total_available = total_results(html, shown=len(products))
outcome.header = search_header(html)
if outcome.header:
logger.info("The page's own result header says: %s", outcome.header)
if products and args.mode == "listing":
priced = sum(1 for p in products if p.price is not None)
share = 100.0 * priced / len(products)
logger.info("Price coverage on batch %d: %d/%d (%.0f%%); the measured "
"floor is %d%%.", page_num, priced, len(products), share,
PRICE_FLOOR)
if share < PRICE_FLOOR:
logger.warning(
"Only %.0f%% of batch %d carries a price, against a floor of "
"%d%% measured across 14 captures where the lowest was 95%%. "
"That is the read breaking rather than the listing being "
"unusual.", share, page_num, PRICE_FLOOR)
# How much of this batch the page's own structured data covered.
# Measured against the rows that COULD carry it: a walked row comes
# from a rendered card, which publishes none, so including those would
# make the share fall as the walk succeeds.
eligible = [p for p in products
if p.price_source in ("jsonld", "static")]
enriched = sum(1 for p in eligible if p.price_source == "jsonld")
if enriched and eligible:
enrich_share = 100.0 * enriched / len(eligible)
logger.info("Structured-data coverage on the served batch: %d/%d "
"(%.0f%%). Measured range across captures: 66%% "
"(paris) to 99%% (toronto).", enriched, len(eligible),
enrich_share)
if enrich_share < ENRICHMENT_FLOOR:
logger.warning(
"Structured coverage is %.0f%%, below the %d%% floor. A "
"low share can mean the served list and the JSON-LD were "
"rejected as misaligned, which is on purpose when the two "
"views disagree.", enrich_share, ENRICHMENT_FLOOR)
else:
logger.info("This listing published no structured data -- normal "
"for jobs, services and community, which ship no "
"JSON-LD ItemList at all.")
with_geo = sum(1 for p in products if p.latitude is not None)
logger.info("Coordinates on batch %d: %d/%d. Craigslist publishes "
"(0,0) as its placeholder outside North America and this "
"parser reports those as null.",
page_num, with_geo, len(products))
if not products:
debug_html = f"{args.out}_page{page_num}_debug.html"
with open(debug_html, "w", encoding="utf-8") as f:
f.write(html)
try:
session.driver.save_screenshot(f"{args.out}_page{page_num}_debug.png")
except WebDriverException as e:
logger.warning("Could not capture screenshot: %s", e)
logger.warning("0 rows parsed — saved what the browser actually saw to "
"%s.", debug_html)
outcome.products = products
outcome.final_url = final_url
return outcome
def scrape(args) -> int:
outcomes: List[PageOutcome] = []
seen_keys = set()
blocked = False
# Both modes are one row per product, so `sku` is the key for both.
dedupe_key = "sku"
# Only --mode posting is single-page: one advert is the whole job, while
# a listing run walks a list. Mirrors playwright_scraper.
stop_reason = "single_page_mode" if args.mode == "posting" else "completed"
pool = proxy_pool_from_args(args)
if pool and args.cdp_endpoint:
logger.warning("Ignoring --proxy/--proxy-file: with --cdp-endpoint the "
"remote browser has its own exit, and layering a second "
"proxy on top would contradict it.")
pool = None
if args.concurrency > 1: