Repository navigation
Expand file tree
/
Copy pathpage_flow.py
More file actions
462 lines (387 loc) · 21.2 KB
/
Copy pathpage_flow.py
File metadata and controls
462 lines (387 loc) · 21.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
460
461
462
"""
page_flow.py
------------
Amazon's page-state policy, shared by all three engines.
Why this module exists, when the rest of this family keeps each engine
self-contained: Amazon answers a request in five different ways, and four of
them want a different response.
ok parse it
waf_challenge wait — the browser clears it itself, for free
throttled retry, then a different exit
captcha solve it (this is the paid path)
signin stop; no proxy or solve changes it
On top of that, two page kinds need coaxing before they hold their content:
a best-seller grid lazy-loads 30 -> 50 cards, and a detail page is served in
two variants, one of which carries no reviews at all and cannot be talked
into it without a fresh session.
Three copies of that would drift, and the drift would be silent — an engine
that treats the WAF interstitial as a block reports exit 3 where its twin
reports exit 0 on the same page. The family already shares
output_writer.finish_run() for exactly this reason ("all three must agree on
exit codes, run status, and whether a run crashes or spends money"); this is
the same argument applied to the decisions that come before it.
The functions here are either pure or driven through small callables, so
each engine passes its own driver's primitives and keeps its browser
plumbing to itself:
count(selector) -> int how many elements match
page_height() -> int document.body.scrollHeight
scroll_to_bottom() -> None jump to the end of the document
scroll_into_view(selector) -> None best-effort, may be a no-op
sleep(ms) -> None the driver's own wait
content() -> Optional[str] current HTML, None if unavailable
current_url() -> str the URL the browser is on
Deliberately no `evaluate(js)`: passing JavaScript from here would decide its
dialect for every driver, and they disagree — Playwright and pyppeteer take
`() => expr`, while Selenium's execute_script takes `return expr;`. Naming
the OPERATION instead keeps this module free of any driver's flavour.
Every value here is measured; the numbers are in the comments and in the
README, and the measurements are dated because Amazon's markup will move.
"""
import logging
from typing import List, Optional
from product_parser import detect_page_state, listing_kind
logger = logging.getLogger("page_flow")
# What "the page has painted" means, per mode. The listing form deliberately
# matches the PRIMARY anchors and not `a[href*="/dp/"]`: a captured search
# page carries 143 such links for 22 organic tiles, most of them in carousels
# that render before the grid does, so counting those would call the page
# ready while the results were still empty.
READY_SELECTOR_LISTING = ('div[data-component-type="s-search-result"][data-asin], '
'[id^="p13n-asin-index-"]')
READY_SELECTOR_PRODUCT = "#productTitle"
READY_SELECTOR_REVIEWS = 'div[data-hook="reviewContainer"]'
# Ordered most-durable first, per the family rule: a standards-based signal
# before a build artefact. `<link rel="next">` is FIRST and is known to be
# ABSENT — checked on both listing kinds on 2026-09-08, zero matches. It is
# kept because it costs one query and is the form that would survive a
# redesign, but it is explicitly not what makes pagination work here:
# `a.s-pagination-next` is a class Amazon can rename any day, and
# product_parser.page_url() is what actually backs pagination up.
NEXT_PAGE_SELECTOR = ("link[rel='next'], a.s-pagination-next, "
"a[aria-label*='next page'], li.a-last > a")
# How many primary anchors must appear before a LISTING counts as loaded
# rather than as a lucky single match. Must be > 1: waiting for one resolves
# on an unrelated element long before the grid paints. Five, against measured
# floors of 16 organic tiles on amazon.com, 16 on amazon.de, 60 on
# amazon.co.jp and 30-before-scroll on a best-seller grid.
MIN_CARD_MATCHES = 5
# How long to let the AWS WAF interstitial clear itself. Measured at 3.9s on
# amazon.co.uk; 25s is generous for a slow exit and still bounded, because
# every remote call in this project is bounded and an unbounded wait here
# would hang a run on a challenge that is never going to resolve.
WAF_WAIT_SECONDS = 25
# The container each mode's content hangs off, used to pull a lazy-load
# trigger precisely instead of jumping past it.
HYDRATION_ANCHORS = {
"reviews": "#reviewsMedley, #customerReviews",
"product": "#feature-bullets, #altImages",
}
# A listing gets the full 20s: a grid can genuinely be slow to paint. A
# detail page gets 8s, because its content is either in the markup that
# arrived or it is not, and the remaining 12s would be spent waiting for a
# variant that is never going to fill itself.
CONTENT_TIMEOUT_MS = {"listing": 20000, "product": 8000, "reviews": 8000}
# Tracking parameters on Amazon's own pagination links. None of them select
# content — they are search-session and click-attribution ids — so a strict
# URL comparison would decide the site's link disagrees with the ?page=N
# convention on every single run, and every run would refuse --concurrency.
TRACKING_PARAMS = {"xpid", "qid", "ref", "ref_", "_encoding", "sprefix", "crid",
"sr", "th", "psc", "linkCode", "tag"}
def ready_selector(mode: str) -> str:
return {"product": READY_SELECTOR_PRODUCT,
"reviews": READY_SELECTOR_REVIEWS}.get(mode, READY_SELECTOR_LISTING)
def min_matches(mode: str) -> int:
"""How many readiness anchors mean "loaded", for this mode.
A listing needs several. A detail page has exactly one `#productTitle`,
so requiring more than one would time out on every successful fetch —
the threshold has to follow the mode or it silently inverts.
"""
return MIN_CARD_MATCHES if mode == "listing" else 0
def content_timeout_ms(mode: str) -> int:
return CONTENT_TIMEOUT_MS.get(mode, 20000)
def needs_scrolling(mode: str, url: str) -> bool:
"""Whether this page grows, or fills in, as you scroll.
Search results do not: a captured page held the same 16 tiles before and
after scrolling to the bottom, because they are server-rendered. Two page
kinds do, both measured — a best-seller grid goes 30 -> 50, and a detail
page's image strip sits below the fold. Scrolling everything anyway would
add seconds per page for nothing on the most common path.
"""
return mode in ("product", "reviews") or listing_kind(url) == "bestsellers"
def wait_out_waf_challenge(content, current_url, sleep) -> bool:
"""Sit through the AWS WAF interstitial. True if the real page arrived.
Amazon answers a suspicious request with HTTP 202 and a page whose only
content is `challenge.js` plus a script that calls
`AwsWafIntegration.getToken()` and then reloads. There is nothing to
solve and nothing to pay for — a real browser produces the token itself.
Measured clearing in 3.9s on amazon.co.uk on 2026-09-08.
This is the single most important difference from the rest of this
family: reporting it as a block (exit 3) would report a block that does
not exist, and handing it to the captcha solver would bill for a
challenge no solver can answer.
"""
remaining, seen, waited = WAF_WAIT_SECONDS, False, 0
while waited < remaining:
html = content()
if html is None:
sleep(500)
waited += 0.5
continue
if detect_page_state(html, url=current_url()) != "waf_challenge":
if seen:
logger.info("AWS WAF challenge cleared itself in ~%.0fs — no "
"solve needed.", waited)
return True
if not seen:
logger.info("AWS WAF challenge page (the browser solves this one "
"itself) — waiting up to %ds for it to reload.",
WAF_WAIT_SECONDS)
seen = True
sleep(1000)
waited += 1
logger.warning("The AWS WAF challenge had not cleared after %ds. Parsing "
"whatever is there; the run will report what it finds.",
WAF_WAIT_SECONDS)
return False
def scroll_until_stable(count, page_height, scroll_to_bottom, sleep,
selector: str, max_rounds: int = 30,
pause_ms: int = 500, stable_rounds: int = 3) -> int:
"""Scroll to the document's end until `selector`'s count stops growing.
Two details were both learned from a run that silently lost data, and
both matter:
* The page is jumped to `document.body.scrollHeight` rather than
wheeled a fixed distance. A 2400px wheel per round stopped three
rounds short of the bottom on a 7600px grid, so the lazy-load
trigger was never reached and the run took 30 of a 50-card chart
while looking settled — the ranks made it visible (1-30, then page 2
starting at 51).
* The count must hold still for `stable_rounds` rounds, not one.
Amazon's next batch takes longer to arrive than a single pause, so
one quiet round is not the end of the content.
Bounded by max_rounds so a page that appends forever cannot hold the run.
"""
previous, previous_height, unchanged = -1, -1, 0
for _ in range(max_rounds):
current = count(selector)
height = page_height()
if current == previous and height == previous_height:
unchanged += 1
if unchanged >= stable_rounds:
return current
else:
unchanged = 0
previous, previous_height = current, height
scroll_to_bottom()
sleep(pause_ms)
logger.info("Content was still growing after %d scroll rounds (%d matches) "
"— continuing with what is loaded.", max_rounds, previous)
return previous
def hydrate(count, page_height, scroll_to_bottom, sleep, scroll_into_view,
mode: str, selector: str, threshold: int, attempts: int = 3) -> int:
"""Pull the lazy-load trigger for this mode, and confirm it fired.
Scrolls the mode's own anchor into view first, because Amazon hydrates
some widgets from an intersection observer on a specific element and
jumping straight to the end of the document flies past it without ever
intersecting it. Then the ordinary bottom-ward pass, for anything below.
"""
anchor = HYDRATION_ANCHORS.get(mode)
for attempt in range(1, attempts + 1):
if anchor:
scroll_into_view(anchor)
loaded = scroll_until_stable(count, page_height, scroll_to_bottom,
sleep, selector)
if loaded > threshold:
return loaded
if attempt < attempts:
logger.info("Nothing matched %s yet (attempt %d/%d) — scrolling "
"its anchor into view again.", selector, attempt, attempts)
sleep(1500)
return count(selector)
# Why an empty review widget needs a NEW SESSION rather than a reload or a
# scroll. Three measurements, in the order they were taken, because the first
# two conclusions were wrong:
#
# 1. Amazon serves two variants of the same /dp/ URL — one with the reviews
# in the initial markup (13 containers, ~466 [data-hook] elements, 17.5k
# page height), one where the widget is present but empty (5
# [data-hook] elements, 10.8k height). Six scroll passes left the second
# at zero, so it is not a lazy load.
# 2. Reloading does not help: a review-less context stayed review-less
# across six consecutive navigations (0,0,0,0,0,0). The variant is
# sticky to the SESSION.
# 3. A fresh context does help: 3 of 5 new contexts returned the full page,
# and 4 of 8 in an earlier sample.
#
# So the variant is assigned per cookie jar, and a new browser is the only
# thing that re-rolls it — the same tool a proxy rotation uses, for the same
# reason: a session cannot be talked out of what it has already been
# assigned. Over a remote CDP endpoint the profile's cookies are not ours to
# discard, so there a review-less profile stays review-less and a different
# `pid` is the answer.
# How many sessions to spend re-rolling the served variant, and how many
# scroll passes to spend before giving up on one. Both are measured, not
# guessed.
#
# The variant hit rate is roughly 55%: 4 of 8 fresh contexts in one sample,
# 3 of 5 in another, and 5 of 9 across three Selenium configurations
# (headless, headless without the automation flag, and headful — the rate did
# not depend on any of them, which is worth knowing because it rules out the
# obvious suspicion that headless is being singled out). At that rate three
# sessions still come back empty about one run in eight, which is too often
# for a mode anyone would put in a pipeline; five bring it under one in fifty.
#
# This is deliberately NOT --retries. That flag is the user's budget for
# transient failures, and spending it on a coin flip Amazon is holding would
# mean a run that asked for one retry gets one chance at the content.
VARIANT_REROLL_ATTEMPTS = 5
# Scroll passes per session. One for reviews, because scrolling was measured
# NOT to be what fills the empty variant — six passes left it at zero — so
# further passes only add seconds to a session that is already lost. Three
# elsewhere, where lazy loading is real and a pass genuinely adds cards.
HYDRATE_ATTEMPTS = {"reviews": 1}
# Chromium's own names for "the proxy is the problem, not the site". Matched
# on an exception's TEXT because every driver here surfaces them as a generic
# error: Playwright as `Error`, Selenium inside a WebDriverException, and
# pyppeteer as one of several types.
#
# Here rather than three times over, which is how it was: each engine
# declared its own copy of this tuple and inlined its own match. They were
# byte-identical when this was written, and a tuple that three files must
# keep identical is a drift waiting to happen — the same argument that put
# the retry and page-state policy in this module.
PROXY_ERROR_MARKERS = (
"ERR_PROXY_CONNECTION_FAILED", # nothing listening / refused
"ERR_TUNNEL_CONNECTION_FAILED", # CONNECT rejected by the proxy
"ERR_PROXY_AUTH_UNSUPPORTED", # auth scheme we cannot satisfy
"ERR_PROXY_AUTH_REQUESTED", # credentials missing or wrong
"ERR_UNEXPECTED_PROXY_AUTH",
"ERR_PROXY_CERTIFICATE_INVALID",
)
def proxy_failure(exc) -> str:
"""The Chromium proxy-error name in `exc`, or "" if it is not one.
Telling this apart from an ordinary timeout is the whole point, because
the two want OPPOSITE responses: a timeout deserves another try at the
same exit, while an unusable exit deserves a different one — retrying it
unchanged just spends the budget on a proxy that is not going to answer.
Takes the exception (or anything str()-able) rather than pre-extracted
text, so no caller can forget to stringify a driver's exception type.
"""
text = str(exc)
for marker in PROXY_ERROR_MARKERS:
if marker in text:
return marker
return ""
# What each driver raises once the browser behind it is gone. Measured
# 2026-11-27 by killing a local Chromium under each driver. Over CDP,
# Playwright still reports page.is_closed() False and browser.is_connected()
# True afterwards, so the exception is the only reliable sign. A page that is
# merely navigating raises something else ("page is navigating", "Execution
# context was destroyed") and must stay a quiet retry.
BROWSER_GONE_TYPES = ("TargetClosedError", "InvalidSessionIdException")
BROWSER_GONE_MARKERS = (
"Target page, context or browser has been closed", # Playwright
"Session closed. Most likely the page has been closed", # pyppeteer
"invalid session id", # Selenium
)
def browser_gone(exc) -> bool:
"""True when `exc` means the browser itself is gone, not a busy page.
A transport failure: the page that was being read is a failed page, the
rows gathered before it are kept, and no retry on the same browser can
help.
"""
if type(exc).__name__ in BROWSER_GONE_TYPES:
return True
text = str(exc)
return any(marker in text for marker in BROWSER_GONE_MARKERS)
# How many paid captcha solves one page may buy, across EVERY attempt at it:
# transport retries, throttle reloads and rotations to another exit alike.
# Before this existed the solve sat inside the per-exit loop with nothing
# counting it, so a page that kept coming back behind a captcha bought one
# solve per exit: measured with a stubbed solver, a pool of three exits and
# the default --proxy-block-retries 2, ONE page cost THREE solves. A fresh
# exit is a reason to re-fetch, not a fresh allowance.
SOLVES_PER_PAGE = 1
class SolveBudget:
"""The per-page allowance of paid solves. Create one per page; never
reset it on a retry or a rotation.
`spend()` is called immediately BEFORE a purchase and answers whether the
purchase may go ahead, so the check and the count cannot drift apart: a
caller that forgot to count would also have forgotten to ask.
"""
def __init__(self, limit: int = SOLVES_PER_PAGE):
self.limit = limit
self.spent = 0
def spend(self) -> bool:
if self.spent >= self.limit:
logger.warning("This page has already used its captcha-solve "
"allowance (%d) — not buying another. The run "
"will report what the page holds.", self.limit)
return False
self.spent += 1
return True
def numeric_arg_errors(*, pages: int, retries: int, retry_delay: float,
delay: float, concurrency: Optional[int] = None,
min_score: Optional[float] = None,
proxy_block_retries: Optional[int] = None) -> List[str]:
"""Every out-of-range numeric argument, as messages. Pure, so it is
testable without building an argparse parser.
Here rather than in each engine because a budget of zero is this module's
business: `session_attempts` and `hydrate_attempts` multiply --retries
into a real number of attempts, so the module that decides what a retry
budget MEANS is the one that should say when it is not a budget.
Each of these was accepted silently before, and two of them made the run
lie about what it did:
--retries 0 the navigation loop is `range(1, retries + 1)`, so it never
runs. Worse than skipping the fetch: `load_failed` is
pre-set to False just above it, so nothing is recorded as
having gone wrong and the run proceeds to classify an
unfetched page. It reports success for work it never did.
--pages 0 page 1 is fetched outside the page loop, so it is fetched
anyway, and the sidecar comes back with pages_requested=0
against pages_completed=1 — arithmetic no consumer can
read.
negative delays `time.sleep` raises on a negative value, so this became
a traceback (exit 1) on a line reached only after a fetch.
--concurrency is NOT in that list: the engine that has it already clamps
with max(1, ...), so 0 is merely rounded up rather than accepted. It is
still validated here so that a caller who typed a negative number is
told rather than quietly given one worker, and so the three engines
answer the same way about the same input.
"""
errors = []
if pages < 1:
errors.append("--pages must be at least 1 (page 1 is fetched "
"regardless, so 0 would report pages_requested=0 "
"against pages_completed=1)")
if retries < 1:
errors.append("--retries must be at least 1: it is the number of "
"attempts, not the number of RE-tries, so 0 would "
"skip the fetch entirely and report nothing wrong")
if retry_delay < 0:
errors.append("--retry-delay cannot be negative")
if delay < 0:
errors.append("--delay cannot be negative")
if concurrency is not None and concurrency < 1:
errors.append("--concurrency must be at least 1")
# 0 is legal here (it counts RE-tries from another exit), but a negative
# value made the per-exit loop run zero times, so the page was never
# fetched and the run went on to judge a page it never loaded.
if proxy_block_retries is not None and proxy_block_retries < 0:
errors.append("--proxy-block-retries cannot be negative")
if min_score is not None and not 0.0 <= min_score <= 1.0:
errors.append("--min-score is a reCAPTCHA v3 score and must be "
"between 0.0 and 1.0")
return errors
def session_attempts(mode: str, retries: int) -> int:
"""How many browser sessions one page may consume.
Reviews get their own budget because their failure is a served variant
rather than a transient fault; see VARIANT_REROLL_ATTEMPTS.
"""
if mode == "reviews":
return max(retries, VARIANT_REROLL_ATTEMPTS)
return retries
def hydrate_attempts(mode: str) -> int:
return HYDRATE_ATTEMPTS.get(mode, 3)
VARIANT_RETRY_NOTE = (
"This session was served the variant of the page that carries no such "
"content — starting a fresh browser session, which is what re-rolls it")