lncrawl-scraper 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- lncrawl_scraper-0.1.0.dist-info/METADATA +478 -0
- lncrawl_scraper-0.1.0.dist-info/RECORD +29 -0
- lncrawl_scraper-0.1.0.dist-info/WHEEL +4 -0
- lncrawl_scraper-0.1.0.dist-info/licenses/LICENSE +201 -0
- scraper/__init__.py +46 -0
- scraper/_engine/__init__.py +398 -0
- scraper/_engine/challenges/__init__.py +13 -0
- scraper/_engine/challenges/base.py +42 -0
- scraper/_engine/challenges/cloudflare_v1.py +243 -0
- scraper/_engine/challenges/cloudflare_v2.py +158 -0
- scraper/_engine/challenges/cloudflare_v3.py +199 -0
- scraper/_engine/challenges/interpreter.py +98 -0
- scraper/_engine/challenges/turnstile.py +55 -0
- scraper/_engine/config.py +113 -0
- scraper/_engine/exceptions.py +41 -0
- scraper/_engine/impersonate.py +107 -0
- scraper/_engine/proxy_manager.py +83 -0
- scraper/_engine/session.py +69 -0
- scraper/_engine/stealth.py +164 -0
- scraper/_engine/tls.py +100 -0
- scraper/_engine/user_agent.py +406 -0
- scraper/_utils/__init__.py +0 -0
- scraper/_utils/event_lock.py +38 -0
- scraper/_utils/file_tools.py +31 -0
- scraper/_utils/url_tools.py +33 -0
- scraper/config.py +60 -0
- scraper/py.typed +0 -0
- scraper/session.py +237 -0
- scraper/soup.py +512 -0
|
@@ -0,0 +1,478 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: lncrawl-scraper
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: HTTP scraper with Cloudflare bypass, browser fingerprint impersonation, stealth mode, proxy support, and a null-safe BeautifulSoup wrapper.
|
|
5
|
+
Project-URL: Homepage, https://github.com/lncrawl/scraper
|
|
6
|
+
Project-URL: Repository, https://github.com/lncrawl/scraper
|
|
7
|
+
Project-URL: Issues, https://github.com/lncrawl/scraper/issues
|
|
8
|
+
Project-URL: Changelog, https://github.com/lncrawl/scraper/blob/main/CHANGELOG.md
|
|
9
|
+
Author-email: Sudipta Chandra <dipu.sudipta@gmail.com>
|
|
10
|
+
License: Apache License
|
|
11
|
+
Version 2.0, January 2004
|
|
12
|
+
http://www.apache.org/licenses/
|
|
13
|
+
|
|
14
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
15
|
+
|
|
16
|
+
1. Definitions.
|
|
17
|
+
|
|
18
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
19
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
20
|
+
|
|
21
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
22
|
+
the copyright owner that is granting the License.
|
|
23
|
+
|
|
24
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
25
|
+
other entities that control, are controlled by, or are under common
|
|
26
|
+
control with that entity. For the purposes of this definition,
|
|
27
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
28
|
+
direction or management of such entity, whether by contract or
|
|
29
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
30
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
31
|
+
|
|
32
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
33
|
+
exercising permissions granted by this License.
|
|
34
|
+
|
|
35
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
36
|
+
including but not limited to software source code, documentation
|
|
37
|
+
source, and configuration files.
|
|
38
|
+
|
|
39
|
+
"Object" form shall mean any form resulting from mechanical
|
|
40
|
+
transformation or translation of a Source form, including but
|
|
41
|
+
not limited to compiled object code, generated documentation,
|
|
42
|
+
and conversions to other media types.
|
|
43
|
+
|
|
44
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
45
|
+
Object form, made available under the License, as indicated by a
|
|
46
|
+
copyright notice that is included in or attached to the work
|
|
47
|
+
(an example is provided in the Appendix below).
|
|
48
|
+
|
|
49
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
50
|
+
form, that is based on (or derived from) the Work and for which the
|
|
51
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
52
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
53
|
+
of this License, Derivative Works shall not include works that remain
|
|
54
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
55
|
+
the Work and Derivative Works thereof.
|
|
56
|
+
|
|
57
|
+
"Contribution" shall mean any work of authorship, including
|
|
58
|
+
the original version of the Work and any modifications or additions
|
|
59
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
60
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
61
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
62
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
63
|
+
means any form of electronic, verbal, or written communication sent
|
|
64
|
+
to the Licensor or its representatives, including but not limited to
|
|
65
|
+
communication on electronic mailing lists, source code control systems,
|
|
66
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
67
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
68
|
+
excluding communication that is conspicuously marked or otherwise
|
|
69
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
70
|
+
|
|
71
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
72
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
73
|
+
subsequently incorporated within the Work.
|
|
74
|
+
|
|
75
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
76
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
77
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
78
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
79
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
80
|
+
Work and such Derivative Works in Source or Object form.
|
|
81
|
+
|
|
82
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
83
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
84
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
85
|
+
(except as stated in this section) patent license to make, have made,
|
|
86
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
87
|
+
where such license applies only to those patent claims licensable
|
|
88
|
+
by such Contributor that are necessarily infringed by their
|
|
89
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
90
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
91
|
+
institute patent litigation against any entity (including a
|
|
92
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
93
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
94
|
+
or contributory patent infringement, then any patent licenses
|
|
95
|
+
granted to You under this License for that Work shall terminate
|
|
96
|
+
as of the date such litigation is filed.
|
|
97
|
+
|
|
98
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
99
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
100
|
+
modifications, and in Source or Object form, provided that You
|
|
101
|
+
meet the following conditions:
|
|
102
|
+
|
|
103
|
+
(a) You must give any other recipients of the Work or
|
|
104
|
+
Derivative Works a copy of this License; and
|
|
105
|
+
|
|
106
|
+
(b) You must cause any modified files to carry prominent notices
|
|
107
|
+
stating that You changed the files; and
|
|
108
|
+
|
|
109
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
110
|
+
that You distribute, all copyright, patent, trademark, and
|
|
111
|
+
attribution notices from the Source form of the Work,
|
|
112
|
+
excluding those notices that do not pertain to any part of
|
|
113
|
+
the Derivative Works; and
|
|
114
|
+
|
|
115
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
116
|
+
distribution, then any Derivative Works that You distribute must
|
|
117
|
+
include a readable copy of the attribution notices contained
|
|
118
|
+
within such NOTICE file, excluding those notices that do not
|
|
119
|
+
pertain to any part of the Derivative Works, in at least one
|
|
120
|
+
of the following places: within a NOTICE text file distributed
|
|
121
|
+
as part of the Derivative Works; within the Source form or
|
|
122
|
+
documentation, if provided along with the Derivative Works; or,
|
|
123
|
+
within a display generated by the Derivative Works, if and
|
|
124
|
+
wherever such third-party notices normally appear. The contents
|
|
125
|
+
of the NOTICE file are for informational purposes only and
|
|
126
|
+
do not modify the License. You may add Your own attribution
|
|
127
|
+
notices within Derivative Works that You distribute, alongside
|
|
128
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
129
|
+
that such additional attribution notices cannot be construed
|
|
130
|
+
as modifying the License.
|
|
131
|
+
|
|
132
|
+
You may add Your own copyright statement to Your modifications and
|
|
133
|
+
may provide additional or different license terms and conditions
|
|
134
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
135
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
136
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
137
|
+
the conditions stated in this License.
|
|
138
|
+
|
|
139
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
140
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
141
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
142
|
+
this License, without any additional terms or conditions.
|
|
143
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
144
|
+
the terms of any separate license agreement you may have executed
|
|
145
|
+
with Licensor regarding such Contributions.
|
|
146
|
+
|
|
147
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
148
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
149
|
+
except as required for reasonable and customary use in describing the
|
|
150
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
151
|
+
|
|
152
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
153
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
154
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
155
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
156
|
+
implied, including, without limitation, any warranties or conditions
|
|
157
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
158
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
159
|
+
appropriateness of using or redistributing the Work and assume any
|
|
160
|
+
risks associated with Your exercise of permissions under this License.
|
|
161
|
+
|
|
162
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
163
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
164
|
+
unless required by applicable law (such as deliberate and grossly
|
|
165
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
166
|
+
liable to You for damages, including any direct, indirect, special,
|
|
167
|
+
incidental, or consequential damages of any character arising as a
|
|
168
|
+
result of this License or out of the use or inability to use the
|
|
169
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
170
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
171
|
+
other commercial damages or losses), even if such Contributor
|
|
172
|
+
has been advised of the possibility of such damages.
|
|
173
|
+
|
|
174
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
175
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
176
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
177
|
+
or other liability obligations and/or rights consistent with this
|
|
178
|
+
License. However, in accepting such obligations, You may act only
|
|
179
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
180
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
181
|
+
defend, and hold each Contributor harmless for any liability
|
|
182
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
183
|
+
of your accepting any such warranty or additional liability.
|
|
184
|
+
|
|
185
|
+
END OF TERMS AND CONDITIONS
|
|
186
|
+
|
|
187
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
188
|
+
|
|
189
|
+
To apply the Apache License to your work, attach the following
|
|
190
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
191
|
+
replaced with your own identifying information. (Don't include
|
|
192
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
193
|
+
comment syntax for the file format. We also recommend that a
|
|
194
|
+
file or class name and description of purpose be included on the
|
|
195
|
+
same "printed page" as the copyright notice for easier
|
|
196
|
+
identification within third-party archives.
|
|
197
|
+
|
|
198
|
+
Copyright [yyyy] [name of copyright owner]
|
|
199
|
+
|
|
200
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
201
|
+
you may not use this file except in compliance with the License.
|
|
202
|
+
You may obtain a copy of the License at
|
|
203
|
+
|
|
204
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
205
|
+
|
|
206
|
+
Unless required by applicable law or agreed to in writing, software
|
|
207
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
208
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
209
|
+
See the License for the specific language governing permissions and
|
|
210
|
+
limitations under the License.
|
|
211
|
+
License-File: LICENSE
|
|
212
|
+
Keywords: beautifulsoup,cloudflare,http,scraper,stealth
|
|
213
|
+
Classifier: Development Status :: 3 - Alpha
|
|
214
|
+
Classifier: Intended Audience :: Developers
|
|
215
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
216
|
+
Classifier: Programming Language :: Python :: 3
|
|
217
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
218
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
219
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
220
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
221
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
222
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
223
|
+
Classifier: Topic :: Internet :: WWW/HTTP
|
|
224
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
225
|
+
Requires-Python: >=3.9
|
|
226
|
+
Requires-Dist: beautifulsoup4>=4.12
|
|
227
|
+
Requires-Dist: brotli>=1.0
|
|
228
|
+
Requires-Dist: lxml>=4.9
|
|
229
|
+
Requires-Dist: quickjs>=1.19.4
|
|
230
|
+
Requires-Dist: requests[socks]>=2.28
|
|
231
|
+
Provides-Extra: image
|
|
232
|
+
Requires-Dist: pillow>=10.0; extra == 'image'
|
|
233
|
+
Provides-Extra: impersonate
|
|
234
|
+
Requires-Dist: curl-cffi>=0.7; extra == 'impersonate'
|
|
235
|
+
Description-Content-Type: text/markdown
|
|
236
|
+
|
|
237
|
+
# LNCrawl Scraper
|
|
238
|
+
|
|
239
|
+
[](https://github.com/lncrawl/scraper/actions/workflows/ci.yml)
|
|
240
|
+
[](https://github.com/lncrawl/scraper/tree/python-coverage-comment-action-data)
|
|
241
|
+
[](https://pypi.org/project/lncrawl-scraper/)
|
|
242
|
+
|
|
243
|
+
HTTP scraper with Cloudflare bypass, browser fingerprint impersonation, stealth mode, proxy support, and a null-safe BeautifulSoup wrapper.
|
|
244
|
+
|
|
245
|
+
## Features
|
|
246
|
+
|
|
247
|
+
- **Cloudflare bypass** — handles CF challenges v1, v2, v3, and Turnstile transparently
|
|
248
|
+
- **Browser fingerprint impersonation** — optional `curl_cffi` transport that
|
|
249
|
+
reproduces a real Chrome/Firefox TLS (JA3/JA4) **and** HTTP/2 fingerprint
|
|
250
|
+
- **Browser-assisted clearance** — reuse a `cf_clearance` cookie solved by a real
|
|
251
|
+
browser for managed-challenge / Turnstile sites
|
|
252
|
+
- **Accurate Client Hints** — `sec-ch-ua` / `sec-fetch-*` derived from the chosen UA
|
|
253
|
+
- **Stealth mode** — human-like delays, randomized headers, browser quirks
|
|
254
|
+
- **Proxy support** — round-robin proxy rotation with Tor integration and direct fallback
|
|
255
|
+
- **Rate limiting** — configurable per-request intervals and concurrency cap
|
|
256
|
+
- **`PageSoup`** — null-safe BeautifulSoup wrapper; selection methods never return `None`
|
|
257
|
+
- **HTTP helpers** — `get_soup`, `get_json`, `get_image`, `get_file`, and more
|
|
258
|
+
|
|
259
|
+
## Installation
|
|
260
|
+
|
|
261
|
+
```bash
|
|
262
|
+
pip install lncrawl-scraper
|
|
263
|
+
|
|
264
|
+
# optional extras:
|
|
265
|
+
pip install "lncrawl-scraper[impersonate]" # browser TLS/HTTP-2 impersonation (curl_cffi)
|
|
266
|
+
pip install "lncrawl-scraper[image]" # get_image() support (Pillow)
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
## Quick start
|
|
270
|
+
|
|
271
|
+
```python
|
|
272
|
+
from scraper import Scraper
|
|
273
|
+
|
|
274
|
+
s = Scraper(origin="https://example.com")
|
|
275
|
+
|
|
276
|
+
# HTML
|
|
277
|
+
soup = s.get_soup("https://example.com/page")
|
|
278
|
+
title = soup.select_one("h1.title").text # "" if not found, never raises
|
|
279
|
+
links = [a["href"] for a in soup.select("a")]
|
|
280
|
+
|
|
281
|
+
# JSON
|
|
282
|
+
data = s.get_json("https://example.com/api/data")
|
|
283
|
+
|
|
284
|
+
# File download
|
|
285
|
+
s.get_file("https://example.com/file.zip", output_file="file.zip")
|
|
286
|
+
|
|
287
|
+
# Image (returns PIL.Image)
|
|
288
|
+
img = s.get_image("https://example.com/cover.jpg")
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
## Examples
|
|
292
|
+
|
|
293
|
+
Runnable examples live in [`examples/`](examples/) — run any with
|
|
294
|
+
`uv run python examples/<file>.py`.
|
|
295
|
+
|
|
296
|
+
| Example | Shows |
|
|
297
|
+
| ------- | ----- |
|
|
298
|
+
| [01_basic_html.py](examples/01_basic_html.py) | Fetch a page and extract data with `get_soup` / `PageSoup` |
|
|
299
|
+
| [02_pagesoup_parsing.py](examples/02_pagesoup_parsing.py) | PageSoup tour: CSS select, attrs, navigation, XPath |
|
|
300
|
+
| [03_json_api.py](examples/03_json_api.py) | `get_json` / `post_json` and raw `Response` access |
|
|
301
|
+
| [04_files_and_images.py](examples/04_files_and_images.py) | `get_file` (streamed, atomic) and `get_image` (Pillow) |
|
|
302
|
+
| [05_forms_cookies_headers.py](examples/05_forms_cookies_headers.py) | `submit_form`, `set_header`, `set_cookie`, `reset` |
|
|
303
|
+
| [06_configuration.py](examples/06_configuration.py) | `ScraperConfig`, `default_config()`, stealth, browser identity |
|
|
304
|
+
| [07_impersonation.py](examples/07_impersonation.py) | Real browser TLS/HTTP-2 fingerprint via `impersonate` |
|
|
305
|
+
| [08_browser_clearance.py](examples/08_browser_clearance.py) | Reuse a `cf_clearance` solved by a real browser |
|
|
306
|
+
| [09_proxies_and_tor.py](examples/09_proxies_and_tor.py) | Proxy rotation and Tor identity refresh |
|
|
307
|
+
| [10_concurrency_and_abort.py](examples/10_concurrency_and_abort.py) | Threaded fetches and cooperative `abort()` |
|
|
308
|
+
| [11_error_handling.py](examples/11_error_handling.py) | HTTP, Cloudflare, and abort error handling |
|
|
309
|
+
|
|
310
|
+
## Configuration
|
|
311
|
+
|
|
312
|
+
Pass a `ScraperConfig` for full control:
|
|
313
|
+
|
|
314
|
+
```python
|
|
315
|
+
from scraper import Scraper
|
|
316
|
+
from scraper.config import ScraperConfig, ProxyConfig, StealthConfig, BrowserConfig
|
|
317
|
+
|
|
318
|
+
config = ScraperConfig(
|
|
319
|
+
min_request_interval=2.0,
|
|
320
|
+
max_concurrent_requests=1,
|
|
321
|
+
rotate_tls_ciphers=True,
|
|
322
|
+
stealth=StealthConfig(
|
|
323
|
+
enabled=True,
|
|
324
|
+
min_delay=1.0,
|
|
325
|
+
max_delay=3.0,
|
|
326
|
+
human_like_delays=True,
|
|
327
|
+
randomize_headers=True,
|
|
328
|
+
browser_quirks=True,
|
|
329
|
+
),
|
|
330
|
+
proxy=ProxyConfig(
|
|
331
|
+
proxy_urls=["http://proxy1:8080", "http://proxy2:8080"],
|
|
332
|
+
fallback_to_direct=True,
|
|
333
|
+
),
|
|
334
|
+
browser=BrowserConfig(browser="firefox", platform="windows", desktop=True),
|
|
335
|
+
)
|
|
336
|
+
|
|
337
|
+
s = Scraper(origin="https://example.com", config=config)
|
|
338
|
+
```
|
|
339
|
+
|
|
340
|
+
Or start from the library's tuned defaults and tweak:
|
|
341
|
+
|
|
342
|
+
```python
|
|
343
|
+
from scraper import Scraper, default_config
|
|
344
|
+
|
|
345
|
+
config = default_config()
|
|
346
|
+
config.max_concurrent_requests = 4
|
|
347
|
+
s = Scraper(origin="https://example.com", config=config)
|
|
348
|
+
```
|
|
349
|
+
|
|
350
|
+
## Browser fingerprint impersonation
|
|
351
|
+
|
|
352
|
+
A plain `requests` stack has a fixed OpenSSL TLS fingerprint and only speaks
|
|
353
|
+
HTTP/1.1 — both of which modern Cloudflare detects. Set `impersonate` (requires
|
|
354
|
+
the `impersonate` extra) to route requests through `curl_cffi`, reproducing a
|
|
355
|
+
real browser's TLS (JA3/JA4) and HTTP/2 fingerprint:
|
|
356
|
+
|
|
357
|
+
```python
|
|
358
|
+
from scraper import Scraper, default_config
|
|
359
|
+
|
|
360
|
+
config = default_config()
|
|
361
|
+
config.impersonate = "chrome" # or "firefox", "chrome124", "safari", …
|
|
362
|
+
s = Scraper(origin="https://example.com", config=config)
|
|
363
|
+
```
|
|
364
|
+
|
|
365
|
+
The spoofed User-Agent family and Client Hints are aligned with the
|
|
366
|
+
impersonation target automatically.
|
|
367
|
+
|
|
368
|
+
## Browser-assisted clearance
|
|
369
|
+
|
|
370
|
+
For managed challenges / Turnstile that can't be solved headlessly, solve the
|
|
371
|
+
challenge once in a real browser (e.g. `nodriver`/Playwright), then hand the
|
|
372
|
+
`cf_clearance` cookie and the browser's **exact** User-Agent to the session:
|
|
373
|
+
|
|
374
|
+
```python
|
|
375
|
+
s.apply_browser_clearance(
|
|
376
|
+
"https://protected.example.com",
|
|
377
|
+
cf_clearance="<value from the browser>",
|
|
378
|
+
user_agent="<the browser's exact UA>",
|
|
379
|
+
cookies={"__cf_bm": "<optional>"},
|
|
380
|
+
)
|
|
381
|
+
```
|
|
382
|
+
|
|
383
|
+
## `Scraper` API
|
|
384
|
+
|
|
385
|
+
| Method | Description |
|
|
386
|
+
| --------------------------------- | --------------------------------------------------- |
|
|
387
|
+
| `get(url, **kwargs)` | GET request, returns `Response` |
|
|
388
|
+
| `post(url, **kwargs)` | POST request, returns `Response` |
|
|
389
|
+
| `ping(url, timeout=5)` | HEAD request for reachability check |
|
|
390
|
+
| `submit_form(url, data, ...)` | POST with form encoding or multipart |
|
|
391
|
+
| `get_json(url, headers, ...)` | GET and parse response as JSON |
|
|
392
|
+
| `post_json(url, data, ...)` | POST and parse response as JSON |
|
|
393
|
+
| `get_soup(url, headers, ...)` | GET and return a `PageSoup` |
|
|
394
|
+
| `post_soup(url, data, ...)` | POST and return a `PageSoup` |
|
|
395
|
+
| `get_image(url, ...)` | GET and return a `PIL.Image` |
|
|
396
|
+
| `get_file(url, output_file, ...)` | Stream download to file (abort-safe) |
|
|
397
|
+
| `make_soup(data, encoding, ...)` | Parse `Response`, `bytes`, or `str` into `PageSoup` |
|
|
398
|
+
| `set_header(key, value)` | Set a default session header |
|
|
399
|
+
| `set_cookie(name, value)` | Set a session cookie |
|
|
400
|
+
| `reset()` | Clear cookies, headers, and state |
|
|
401
|
+
|
|
402
|
+
## `PageSoup` API
|
|
403
|
+
|
|
404
|
+
`PageSoup` wraps a BeautifulSoup `Tag`. Every selection method returns a `PageSoup` (never `None`); an empty `PageSoup` is falsy and returns safe defaults for all operations.
|
|
405
|
+
|
|
406
|
+
```python
|
|
407
|
+
soup = s.get_soup("https://example.com")
|
|
408
|
+
|
|
409
|
+
# Selection
|
|
410
|
+
soup.select("ul li") # → List[PageSoup]
|
|
411
|
+
soup.select_one(".title") # → PageSoup (empty if not found)
|
|
412
|
+
soup.find("div", class_="content") # → PageSoup
|
|
413
|
+
soup.find_all("a") # → List[PageSoup]
|
|
414
|
+
soup.xpath("//div[@class='body']") # → List[PageSoup]
|
|
415
|
+
soup.closest(".container") # → nearest matching ancestor
|
|
416
|
+
soup.parents(".wrapper") # → generator of matching ancestors
|
|
417
|
+
|
|
418
|
+
# Attribute access
|
|
419
|
+
el["href"] # get_attr shorthand, returns "" if missing
|
|
420
|
+
el.get_attr("src", default="/")
|
|
421
|
+
el.has_attr("data-id")
|
|
422
|
+
|
|
423
|
+
# Text / HTML
|
|
424
|
+
el.text # stripped text, always str
|
|
425
|
+
el.get_text(separator="\n")
|
|
426
|
+
el.inner_html
|
|
427
|
+
el.outer_html
|
|
428
|
+
|
|
429
|
+
# Navigation
|
|
430
|
+
el.parent
|
|
431
|
+
el.children # List[PageSoup], excludes text nodes
|
|
432
|
+
el.next_sibling
|
|
433
|
+
el.previous_sibling
|
|
434
|
+
|
|
435
|
+
# Mutation
|
|
436
|
+
soup.decompose(".ads") # remove elements matching selector
|
|
437
|
+
el.replace_with(new_el)
|
|
438
|
+
el.append(child)
|
|
439
|
+
```
|
|
440
|
+
|
|
441
|
+
## Development
|
|
442
|
+
|
|
443
|
+
[uv](https://docs.astral.sh/uv/) is required. Clone the repo and install all dependencies including dev extras:
|
|
444
|
+
|
|
445
|
+
```bash
|
|
446
|
+
git clone https://github.com/lncrawl/scraper.git
|
|
447
|
+
cd scraper
|
|
448
|
+
uv sync --all-groups --all-extras
|
|
449
|
+
```
|
|
450
|
+
|
|
451
|
+
Tasks are managed with [poethepoet](https://poethepoet.natn.io/):
|
|
452
|
+
|
|
453
|
+
| Command | Description |
|
|
454
|
+
| --------------------- | ------------------------------------- |
|
|
455
|
+
| `uv run poe lint` | Run ruff + pyright |
|
|
456
|
+
| `uv run poe lint-fix` | Auto-fix ruff violations and reformat |
|
|
457
|
+
| `uv run poe test` | Run the test suite |
|
|
458
|
+
| `uv run poe build` | Lint → test → build wheel |
|
|
459
|
+
| `uv run poe publish` | Build → publish to PyPI |
|
|
460
|
+
|
|
461
|
+
## Testing
|
|
462
|
+
|
|
463
|
+
Tests live in [`tests/`](tests/) and run with [pytest](https://pytest.org):
|
|
464
|
+
|
|
465
|
+
```bash
|
|
466
|
+
uv run poe test
|
|
467
|
+
|
|
468
|
+
# or directly
|
|
469
|
+
uv run pytest
|
|
470
|
+
uv run pytest -v # verbose
|
|
471
|
+
uv run pytest tests/test_dummy.py # a single file
|
|
472
|
+
```
|
|
473
|
+
|
|
474
|
+
Mock HTTP with [responses](https://github.com/getsentry/responses) (a dev dependency) so tests make no real network calls.
|
|
475
|
+
|
|
476
|
+
## License
|
|
477
|
+
|
|
478
|
+
[Apache-2.0](LICENSE)
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
scraper/__init__.py,sha256=oYhV-gAHCFo2X8xI5Re7iF9xX1CQMoAB3baH3_Hr18I,1355
|
|
2
|
+
scraper/config.py,sha256=thdlnZSom9GTL2zHE4Uoejdhtv4m5BiyxPYlzAldv3c,1639
|
|
3
|
+
scraper/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
4
|
+
scraper/session.py,sha256=OpN0ljm9s8FSqJZK20yafHeDsNxMAfhPDgpKGPvkYms,8622
|
|
5
|
+
scraper/soup.py,sha256=D5KNd0jrsAEjpcKKeQFcqdtu0bOA4u0FDD5ZWfbgCpY,16914
|
|
6
|
+
scraper/_engine/__init__.py,sha256=woQ6W0INptjllQNfxDfZ_f5HncPSzuly3Xk7HNMndvE,16246
|
|
7
|
+
scraper/_engine/config.py,sha256=ZTnASYRfI5idPuYn8eOvFm2GvISySRX_osCfBLnUvwU,3637
|
|
8
|
+
scraper/_engine/exceptions.py,sha256=EbUK6u00BUYpdwNZz1N293amIbYtU7EbPDU0Qlxu64I,1319
|
|
9
|
+
scraper/_engine/impersonate.py,sha256=kbY-5ULzS63hdAUnRdNrPhF8qhNXVIo_IcaSTAYXFJ4,4165
|
|
10
|
+
scraper/_engine/proxy_manager.py,sha256=PNiKYb0s5s6kfy7C-SRLKuHLyhSciRWroLFY7pq3Vew,3042
|
|
11
|
+
scraper/_engine/session.py,sha256=8KEu8VGOhuO3EyP1ozbMtpbFYIpVhYo139kSNdP71b8,2238
|
|
12
|
+
scraper/_engine/stealth.py,sha256=UdfgVOBKeBtbMWl4aDD-yDmOIdLPazIDTt0jv8Sid_o,4943
|
|
13
|
+
scraper/_engine/tls.py,sha256=T3f6VL_XhZhBbwOE480B6KLTMhgbm4DIn0qQUUdxDo4,3557
|
|
14
|
+
scraper/_engine/user_agent.py,sha256=lRzs-IM6358gcOo-mNMK33TxufYe4BOnHXQFk01LRlQ,14995
|
|
15
|
+
scraper/_engine/challenges/__init__.py,sha256=-BAmhcqZdmGS5A0XUQ8sOn2Bh14ll_A049OXA6Qa4fU,360
|
|
16
|
+
scraper/_engine/challenges/base.py,sha256=JmZ5l-YkK0UfwdfPki7JnmKBkeyMPe_sBCScFGhcfvU,1557
|
|
17
|
+
scraper/_engine/challenges/cloudflare_v1.py,sha256=4-V8fWPqEKR3JwmD712nceB2ouTXRWxpXtONI1hhR80,8683
|
|
18
|
+
scraper/_engine/challenges/cloudflare_v2.py,sha256=Fi2pVjSm6ESfQkZb3l3CoIEQblKFGX4Yl4y4NJFWn78,5390
|
|
19
|
+
scraper/_engine/challenges/cloudflare_v3.py,sha256=T5B28gLP8g4mIJOmqipmQG5t7eQf_H8-B4N--yx47qU,7526
|
|
20
|
+
scraper/_engine/challenges/interpreter.py,sha256=H1kFOv6WXy4pOES2UWH3gbB1C6WhqlCAjCA1w8pjGAw,3239
|
|
21
|
+
scraper/_engine/challenges/turnstile.py,sha256=sDISgGYwrmspbxEMJ5mORVrwBqZ2hi2VP5jVDaQHPFg,1913
|
|
22
|
+
scraper/_utils/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
23
|
+
scraper/_utils/event_lock.py,sha256=QrhLruorhgLPX_1rRglKwmtkFzgZ5t65NsTX6m9h9JE,964
|
|
24
|
+
scraper/_utils/file_tools.py,sha256=Ox7ovkjQtC3nsjSnl5EwXwJ3hykcdM8ZxiX2BjP-SPg,852
|
|
25
|
+
scraper/_utils/url_tools.py,sha256=3qrA8_hgowvT2fSLBbFcLf2ui4TA9uudCdADbhFdoRQ,861
|
|
26
|
+
lncrawl_scraper-0.1.0.dist-info/METADATA,sha256=qlU63zxTLM2aSBvpqhtShJfRKAR9JX6A1g8ygArBabo,24246
|
|
27
|
+
lncrawl_scraper-0.1.0.dist-info/WHEEL,sha256=mffPy8wBnZQn2VnJUU5jE99KsxaSfiyMHV9Yt0aLVxs,87
|
|
28
|
+
lncrawl_scraper-0.1.0.dist-info/licenses/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
|
|
29
|
+
lncrawl_scraper-0.1.0.dist-info/RECORD,,
|