Repository navigation
Expand file tree
/
Copy pathselenium_scraper.py
More file actions
323 lines (265 loc) · 13.8 KB
/
Copy pathselenium_scraper.py
File metadata and controls
323 lines (265 loc) · 13.8 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
#!/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 (see CLAUDE.md §6):
- Selenium CANNOT use an AUTHENTICATED remote CDP endpoint. 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).
Same input as playwright_scraper.py: `--url`/`--urls-file` (profiles,
posts, Reels or usernames) and `--posts N`, not `--query`/`--category` —
Instagram's search needs a login.
"""
from __future__ import annotations
import argparse
import asyncio
import logging
import os
import sys
import time
from typing import Optional
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 page_flow
import scraper_api_engine
import page_parser as pp
from output_writer import EXIT_BAD_USAGE, EXIT_CRASH
from proxy_pool import Proxy, ProxyPool, ProxyParseError, 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 = 45
READINESS_WAIT_S = 1.5 # see playwright_scraper.READINESS_WAIT_MS
_CHROME_BINARY = os.environ.get("SELENIUM_CHROME_BIN") or os.environ.get("CHROME_BIN")
_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/lidl-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 _positive_int(value: str) -> int:
ivalue = int(value)
if ivalue < 1:
raise argparse.ArgumentTypeError(f"must be a positive integer (got {value!r})")
return ivalue
def build_arg_parser() -> argparse.ArgumentParser:
p = argparse.ArgumentParser(
description="instagram.com public profile and post scraper (logged out) — Selenium engine",
epilog="Credentials belong in .env / INSTAGRAM_PROXY / TWOCAPTCHA_KEY — never on this command line.",
)
p.add_argument("--url", default=None, help="One profile, post or Reel URL, @username or bare username (or set INSTAGRAM_URL) — overrides --urls-file")
p.add_argument("--urls-file", default=None)
p.add_argument("--posts", type=int, default=0, metavar="N", help="Also fetch each profile's N most recent posts (0-12; a logged-out profile page embeds 12, pinned first)")
p.add_argument("--max-results", type=_positive_int, default=100)
p.add_argument("--delay-between-pages", type=float, default=2.0)
p.add_argument("--format", choices=["json", "csv"], default="json")
p.add_argument("--out", default=None)
p.add_argument("--retries", type=int, default=2)
p.add_argument("--retry-delay", type=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=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("--max-solves", type=int, default=8, help="Cap on PAID 2Captcha solves for the whole run (0 = never pay); recorded as solves_spent")
p.add_argument("--min-score", type=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("--scraper-api", action="store_true", help="Fetch through 2Captcha's Scraper API instead of driving this browser (needs TWOCAPTCHA_KEY; routed through INSTAGRAM_CDP_ENDPOINT's Scraping Browser profile when one is set)")
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")
page_flow.add_run_arguments(p)
return p
def _default_out(fmt: str) -> str:
return f"instagram_results.{fmt}"
_resolve_urls = page_flow.resolve_urls # the one implementation is page_flow's
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")
# Instagram localises its alt text ("Photo by ... on October 02, 2026")
# to the browser's language — pin English so rows are comparable across
# machines.
options.add_argument("--lang=en-US")
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)
# chromedriver refuses prefs on an attached (debuggerAddress) browser
options.add_experimental_option("prefs", {"intl.accept_languages": "en-US,en"})
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)
_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, min_score: float = 0.3) -> Optional[dict]:
if policy == "off" or client is None:
return None
log.warning("Local captcha solving is disabled: token delivery is not implemented; no paid task created. "
"Use the Scraping Browser CDP auto-solve integration.")
return {"action": "unsupported_delivery"}
def _page_source(driver) -> str:
try:
return driver.page_source
except WebDriverException:
return ""
def _quit(driver) -> None:
try:
driver.quit()
except Exception: # noqa: BLE001 — cleanup only
pass
class _SeleniumSession:
"""page_flow.PageSession over one Selenium driver (blocking WebDriver
calls inside coroutines; the shared loop runs under asyncio.run)."""
def __init__(self, driver):
self.driver = driver
async def goto(self, url: str) -> Optional[int]:
self.driver.set_page_load_timeout(NAV_TIMEOUT_S)
self.driver.get(url)
time.sleep(READINESS_WAIT_S)
try:
reported = self.driver.execute_script(_STATUS_JS)
return int(reported) if reported else None
except WebDriverException:
return None
async def content(self) -> str:
return _page_source(self.driver)
async def wait(self, seconds: float) -> None:
time.sleep(seconds)
async def close(self) -> None:
_quit(self.driver)
class _SeleniumEngine:
"""page_flow.Engine for Selenium: a fresh driver per URL."""
name = ENGINE_NAME
readiness_s = READINESS_WAIT_S
def __init__(self, args: argparse.Namespace, *, user_agent: Optional[str], client):
self.args, self.user_agent, self.client = args, user_agent, client
async def open(self, proxy) -> _SeleniumSession:
return _SeleniumSession(_build_driver(headless=self.args.headless, proxy=proxy,
cdp_endpoint=self.args.cdp_endpoint, user_agent=self.user_agent))
async def sleep(self, seconds: float) -> None:
time.sleep(seconds)
async def solve_captcha(self, session, *, html: str, url: str):
return _maybe_solve_captcha(html=html, url=url, client=self.client, policy=self.args.solve_captcha,
min_score=self.args.min_score)
def run(args: argparse.Namespace) -> int:
started_at = time.time()
args._solve_budget = page_flow.SolveBudget(args.max_solves)
try:
urls, skipped = _resolve_urls(args)
except ValueError as exc:
print(f"Error: {exc}", file=sys.stderr)
return EXIT_BAD_USAGE
stop = page_flow.validate_common(args, urls=urls, skipped=skipped, print_err=lambda m: print(m, file=sys.stderr))
if stop is not None:
return stop
if args.scraper_api:
args.out = args.out or _default_out(args.format)
return asyncio.run(scraper_api_engine.run(args, urls=urls, started_at=started_at))
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)
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 = 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)
if user_agent:
log.info("Fingerprint applied: user agent %s", user_agent)
try:
return asyncio.run(page_flow.run(_SeleniumEngine(args, user_agent=user_agent, client=client), args, urls=urls,
proxy_pool=proxy_pool, client=client,
started_at=started_at))
except Exception:
log.exception("Unhandled error — this is a crash, not a normal blocked/empty run")
return EXIT_CRASH
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())