feat: Upload the library agent skill
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
# Fetchers basics
|
||||
|
||||
## Introduction
|
||||
Fetchers are classes that do requests or fetch pages in a single-line fashion with many features and return a [Response](#response-object) object. All fetchers have separate session classes to keep the session running (e.g., a browser fetcher keeps the browser open until you finish all requests).
|
||||
|
||||
Fetchers are not wrappers built on top of other libraries. They use these libraries as an engine to request/fetch pages but add features the underlying engines don't have, while still fully leveraging and optimizing them for web scraping.
|
||||
|
||||
## Fetchers Overview
|
||||
|
||||
Scrapling provides three different fetcher classes with their session classes; each fetcher is designed for a specific use case.
|
||||
|
||||
The following table compares them and can be quickly used for guidance.
|
||||
|
||||
|
||||
| Feature | Fetcher | DynamicFetcher | StealthyFetcher |
|
||||
|--------------------|---------------------------------------------------|-----------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------|
|
||||
| Relative speed | 🐇🐇🐇🐇🐇 | 🐇🐇🐇 | 🐇🐇🐇 |
|
||||
| Stealth | ⭐⭐ | ⭐⭐⭐ | ⭐⭐⭐⭐⭐ |
|
||||
| Anti-Bot options | ⭐⭐ | ⭐⭐⭐ | ⭐⭐⭐⭐⭐ |
|
||||
| JavaScript loading | ❌ | ✅ | ✅ |
|
||||
| Memory Usage | ⭐ | ⭐⭐⭐ | ⭐⭐⭐ |
|
||||
| Best used for | Basic scraping when HTTP requests alone can do it | - Dynamically loaded websites <br/>- Small automation<br/>- Small-Mid protections | - Dynamically loaded websites <br/>- Small automation <br/>- Small-Complicated protections |
|
||||
| Browser(s) | ❌ | Chromium and Google Chrome | Chromium and Google Chrome |
|
||||
| Browser API used | ❌ | PlayWright | PlayWright |
|
||||
| Setup Complexity | Simple | Simple | Simple |
|
||||
|
||||
## Parser configuration in all fetchers
|
||||
All fetchers share the same import method, as you will see in the upcoming pages
|
||||
```python
|
||||
>>> from scrapling.fetchers import Fetcher, AsyncFetcher, StealthyFetcher, DynamicFetcher
|
||||
```
|
||||
Then you use it right away without initializing like this, and it will use the default parser settings:
|
||||
```python
|
||||
>>> page = StealthyFetcher.fetch('https://example.com')
|
||||
```
|
||||
If you want to configure the parser ([Selector class](parsing/main_classes.md#selector)) that will be used on the response before returning it for you, then do this first:
|
||||
```python
|
||||
>>> from scrapling.fetchers import Fetcher
|
||||
>>> Fetcher.configure(adaptive=True, keep_comments=False, keep_cdata=False) # and the rest
|
||||
```
|
||||
or
|
||||
```python
|
||||
>>> from scrapling.fetchers import Fetcher
|
||||
>>> Fetcher.adaptive=True
|
||||
>>> Fetcher.keep_comments=False
|
||||
>>> Fetcher.keep_cdata=False # and the rest
|
||||
```
|
||||
Then, continue your code as usual.
|
||||
|
||||
The available configuration arguments are: `adaptive`, `adaptive_domain`, `huge_tree`, `keep_comments`, `keep_cdata`, `storage`, and `storage_args`, which are the same ones you give to the [Selector](parsing/main_classes.md#selector) class. You can display the current configuration anytime by running `<fetcher_class>.display_config()`.
|
||||
|
||||
**Info:** The `adaptive` argument is disabled by default; you must enable it to use that feature.
|
||||
|
||||
### Set parser config per request
|
||||
As you probably understand, the logic above for setting the parser config will apply globally to all requests/fetches made through that class, and it's intended for simplicity.
|
||||
|
||||
If your use case requires a different configuration for each request/fetch, you can pass a dictionary to the request method (`fetch`/`get`/`post`/...) to an argument named `selector_config`.
|
||||
|
||||
## Response Object
|
||||
The `Response` object is the same as the [Selector](parsing/main_classes.md#selector) class, but it has additional details about the response, like response headers, status, cookies, etc., as shown below:
|
||||
```python
|
||||
>>> from scrapling.fetchers import Fetcher
|
||||
>>> page = Fetcher.get('https://example.com')
|
||||
|
||||
>>> page.status # HTTP status code
|
||||
>>> page.reason # Status message
|
||||
>>> page.cookies # Response cookies as a dictionary
|
||||
>>> page.headers # Response headers
|
||||
>>> page.request_headers # Request headers
|
||||
>>> page.history # Response history of redirections, if any
|
||||
>>> page.body # Raw response body as bytes
|
||||
>>> page.encoding # Response encoding
|
||||
>>> page.meta # Response metadata dictionary (e.g., proxy used). Mainly helpful with the spiders system.
|
||||
```
|
||||
All fetchers return the `Response` object.
|
||||
|
||||
**Note:** Unlike the [Selector](parsing/main_classes.md#selector) class, the `Response` class's body is always bytes since v0.4.
|
||||
@@ -0,0 +1,306 @@
|
||||
# Fetching dynamic websites
|
||||
|
||||
`DynamicFetcher` (formerly `PlayWrightFetcher`) provides flexible browser automation with multiple configuration options and built-in stealth improvements.
|
||||
|
||||
As we will explain later, to automate the page, you need some knowledge of [Playwright's Page API](https://playwright.dev/python/docs/api/class-page).
|
||||
|
||||
## Basic Usage
|
||||
You have one primary way to import this Fetcher, which is the same for all fetchers.
|
||||
|
||||
```python
|
||||
>>> from scrapling.fetchers import DynamicFetcher
|
||||
```
|
||||
Check out how to configure the parsing options [here](choosing.md#parser-configuration-in-all-fetchers)
|
||||
|
||||
**Note:** The async version of the `fetch` method is `async_fetch`.
|
||||
|
||||
This fetcher provides three main run options that can be combined as desired.
|
||||
|
||||
Which are:
|
||||
|
||||
### 1. Vanilla Playwright
|
||||
```python
|
||||
DynamicFetcher.fetch('https://example.com')
|
||||
```
|
||||
Using it in that manner will open a Chromium browser and load the page. There are optimizations for speed, and some stealth goes automatically under the hood, but other than that, there are no tricks or extra features unless you enable some; it's just a plain PlayWright API.
|
||||
|
||||
### 2. Real Chrome
|
||||
```python
|
||||
DynamicFetcher.fetch('https://example.com', real_chrome=True)
|
||||
```
|
||||
If you have a Google Chrome browser installed, use this option. It's the same as the first option, but it will use the Google Chrome browser you installed on your device instead of Chromium. This will make your requests look more authentic, so they're less detectable for better results.
|
||||
|
||||
If you don't have Google Chrome installed and want to use this option, you can use the command below in the terminal to install it for the library instead of installing it manually:
|
||||
```commandline
|
||||
playwright install chrome
|
||||
```
|
||||
|
||||
### 3. CDP Connection
|
||||
```python
|
||||
DynamicFetcher.fetch('https://example.com', cdp_url='ws://localhost:9222')
|
||||
```
|
||||
Instead of launching a browser locally (Chromium/Google Chrome), you can connect to a remote browser through the [Chrome DevTools Protocol](https://chromedevtools.github.io/devtools-protocol/).
|
||||
|
||||
|
||||
**Notes:**
|
||||
* There was a `stealth` option here, but it was moved to the `StealthyFetcher` class, as explained on the next page, with additional features since version 0.3.13.
|
||||
* This makes it less confusing for new users, easier to maintain, and provides other benefits, as explained on the [StealthyFetcher page](fetching/stealthy.md).
|
||||
|
||||
## Full list of arguments
|
||||
All arguments for `DynamicFetcher` and its session classes:
|
||||
|
||||
| Argument | Description | Optional |
|
||||
|:-------------------:|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|:--------:|
|
||||
| url | Target url | ❌ |
|
||||
| headless | Pass `True` to run the browser in headless/hidden (**default**) or `False` for headful/visible mode. | ✔️ |
|
||||
| disable_resources | Drop requests for unnecessary resources for a speed boost. Requests dropped are of type `font`, `image`, `media`, `beacon`, `object`, `imageset`, `texttrack`, `websocket`, `csp_report`, and `stylesheet`. | ✔️ |
|
||||
| cookies | Set cookies for the next request. | ✔️ |
|
||||
| useragent | Pass a useragent string to be used. **Otherwise, the fetcher will generate and use a real Useragent of the same browser and version.** | ✔️ |
|
||||
| network_idle | Wait for the page until there are no network connections for at least 500 ms. | ✔️ |
|
||||
| load_dom | Enabled by default, wait for all JavaScript on page(s) to fully load and execute (wait for the `domcontentloaded` state). | ✔️ |
|
||||
| timeout | The timeout (milliseconds) used in all operations and waits through the page. The default is 30,000 ms (30 seconds). | ✔️ |
|
||||
| wait | The time (milliseconds) the fetcher will wait after everything finishes before closing the page and returning the `Response` object. | ✔️ |
|
||||
| page_action | Added for automation. Pass a function that takes the `page` object and does the necessary automation. | ✔️ |
|
||||
| wait_selector | Wait for a specific css selector to be in a specific state. | ✔️ |
|
||||
| init_script | An absolute path to a JavaScript file to be executed on page creation for all pages in this session. | ✔️ |
|
||||
| wait_selector_state | Scrapling will wait for the given state to be fulfilled for the selector given with `wait_selector`. _Default state is `attached`._ | ✔️ |
|
||||
| google_search | Enabled by default, Scrapling will set the referer header as if this request came from a Google search of this website's domain name. | ✔️ |
|
||||
| extra_headers | A dictionary of extra headers to add to the request. _The referer set by the `google_search` argument takes priority over the referer set here if used together._ | ✔️ |
|
||||
| proxy | The proxy to be used with requests. It can be a string or a dictionary with only the keys 'server', 'username', and 'password'. | ✔️ |
|
||||
| real_chrome | If you have a Chrome browser installed on your device, enable this, and the Fetcher will launch and use an instance of your browser. | ✔️ |
|
||||
| locale | Specify user locale, for example, `en-GB`, `de-DE`, etc. Locale will affect `navigator.language` value, `Accept-Language` request header value, as well as number and date formatting rules. Defaults to the system default locale. | ✔️ |
|
||||
| timezone_id | Changes the timezone of the browser. Defaults to the system timezone. | ✔️ |
|
||||
| cdp_url | Instead of launching a new browser instance, connect to this CDP URL to control real browsers through CDP. | ✔️ |
|
||||
| user_data_dir | Path to a User Data Directory, which stores browser session data like cookies and local storage. The default is to create a temporary directory. **Only Works with sessions** | ✔️ |
|
||||
| extra_flags | A list of additional browser flags to pass to the browser on launch. | ✔️ |
|
||||
| additional_args | Additional arguments to be passed to Playwright's context as additional settings, and they take higher priority than Scrapling's settings. | ✔️ |
|
||||
| selector_config | A dictionary of custom parsing arguments to be used when creating the final `Selector`/`Response` class. | ✔️ |
|
||||
| blocked_domains | A set of domain names to block requests to. Subdomains are also matched (e.g., `"example.com"` blocks `"sub.example.com"` too). | ✔️ |
|
||||
| proxy_rotator | A `ProxyRotator` instance for automatic proxy rotation. Cannot be combined with `proxy`. | ✔️ |
|
||||
| retries | Number of retry attempts for failed requests. Defaults to 3. | ✔️ |
|
||||
| retry_delay | Seconds to wait between retry attempts. Defaults to 1. | ✔️ |
|
||||
|
||||
In session classes, all these arguments can be set globally for the session. Still, you can configure each request individually by passing some of the arguments here that can be configured on the browser tab level like: `google_search`, `timeout`, `wait`, `page_action`, `extra_headers`, `disable_resources`, `wait_selector`, `wait_selector_state`, `network_idle`, `load_dom`, `blocked_domains`, `proxy`, and `selector_config`.
|
||||
|
||||
**Notes:**
|
||||
1. The `disable_resources` option made requests ~25% faster in tests for some websites and can help save proxy usage, but be careful with it, as it can cause some websites to never finish loading.
|
||||
2. The `google_search` argument is enabled by default for all requests, making the request appear to come from a Google search page. So, a request for `https://example.com` will set the referer to `https://www.google.com/search?q=example`. Also, if used together, it takes priority over the referer set by the `extra_headers` argument.
|
||||
3. Since version 0.3.13, the `stealth` option has been removed here in favor of the `StealthyFetcher` class, and the `hide_canvas` option has been moved to it. The `disable_webgl` argument has been moved to the `StealthyFetcher` class and renamed as `allow_webgl`.
|
||||
4. If you didn't set a user agent and enabled headless mode, the fetcher will generate a real user agent for the same browser version and use it. If you didn't set a user agent and didn't enable headless mode, the fetcher will use the browser's default user agent, which is the same as in standard browsers in the latest versions.
|
||||
|
||||
|
||||
## Examples
|
||||
|
||||
### Resource Control
|
||||
|
||||
```python
|
||||
# Disable unnecessary resources
|
||||
page = DynamicFetcher.fetch('https://example.com', disable_resources=True) # Blocks fonts, images, media, etc.
|
||||
```
|
||||
|
||||
### Domain Blocking
|
||||
|
||||
```python
|
||||
# Block requests to specific domains (and their subdomains)
|
||||
page = DynamicFetcher.fetch('https://example.com', blocked_domains={"ads.example.com", "tracker.net"})
|
||||
```
|
||||
|
||||
### Network Control
|
||||
|
||||
```python
|
||||
# Wait for network idle (Consider fetch to be finished when there are no network connections for at least 500 ms)
|
||||
page = DynamicFetcher.fetch('https://example.com', network_idle=True)
|
||||
|
||||
# Custom timeout (in milliseconds)
|
||||
page = DynamicFetcher.fetch('https://example.com', timeout=30000) # 30 seconds
|
||||
|
||||
# Proxy support (It can also be a dictionary with only the keys 'server', 'username', and 'password'.)
|
||||
page = DynamicFetcher.fetch('https://example.com', proxy='http://username:password@host:port')
|
||||
```
|
||||
|
||||
### Proxy Rotation
|
||||
|
||||
```python
|
||||
from scrapling.fetchers import DynamicSession, ProxyRotator
|
||||
|
||||
# Set up proxy rotation
|
||||
rotator = ProxyRotator([
|
||||
"http://proxy1:8080",
|
||||
"http://proxy2:8080",
|
||||
"http://proxy3:8080",
|
||||
])
|
||||
|
||||
# Use with session - rotates proxy automatically with each request
|
||||
with DynamicSession(proxy_rotator=rotator, headless=True) as session:
|
||||
page1 = session.fetch('https://example1.com')
|
||||
page2 = session.fetch('https://example2.com')
|
||||
|
||||
# Override rotator for a specific request
|
||||
page3 = session.fetch('https://example3.com', proxy='http://specific-proxy:8080')
|
||||
```
|
||||
|
||||
**Warning:** By default, all browser-based fetchers and sessions use a persistent browser context with a pool of tabs. However, since browsers can't set a proxy per tab, when you use a `ProxyRotator`, the fetcher will automatically open a separate context for each proxy, with one tab per context. Once the tab's job is done, both the tab and its context are closed.
|
||||
|
||||
### Downloading Files
|
||||
|
||||
```python
|
||||
page = DynamicFetcher.fetch('https://raw.githubusercontent.com/D4Vinci/Scrapling/main/images/main_cover.png')
|
||||
|
||||
with open(file='main_cover.png', mode='wb') as f:
|
||||
f.write(page.body)
|
||||
```
|
||||
|
||||
The `body` attribute of the `Response` object always returns `bytes`.
|
||||
|
||||
### Browser Automation
|
||||
This is where your knowledge about [Playwright's Page API](https://playwright.dev/python/docs/api/class-page) comes into play. The function you pass here takes the page object from Playwright's API, performs the desired action, and then the fetcher continues.
|
||||
|
||||
This function is executed immediately after waiting for `network_idle` (if enabled) and before waiting for the `wait_selector` argument, allowing it to be used for purposes beyond automation. You can alter the page as you want.
|
||||
|
||||
In the example below, I used the pages' [mouse events](https://playwright.dev/python/docs/api/class-mouse) to scroll the page with the mouse wheel, then move the mouse.
|
||||
```python
|
||||
from playwright.sync_api import Page
|
||||
|
||||
def scroll_page(page: Page):
|
||||
page.mouse.wheel(10, 0)
|
||||
page.mouse.move(100, 400)
|
||||
page.mouse.up()
|
||||
|
||||
page = DynamicFetcher.fetch('https://example.com', page_action=scroll_page)
|
||||
```
|
||||
Of course, if you use the async fetch version, the function must also be async.
|
||||
```python
|
||||
from playwright.async_api import Page
|
||||
|
||||
async def scroll_page(page: Page):
|
||||
await page.mouse.wheel(10, 0)
|
||||
await page.mouse.move(100, 400)
|
||||
await page.mouse.up()
|
||||
|
||||
page = await DynamicFetcher.async_fetch('https://example.com', page_action=scroll_page)
|
||||
```
|
||||
|
||||
### Wait Conditions
|
||||
|
||||
```python
|
||||
# Wait for the selector
|
||||
page = DynamicFetcher.fetch(
|
||||
'https://example.com',
|
||||
wait_selector='h1',
|
||||
wait_selector_state='visible'
|
||||
)
|
||||
```
|
||||
This is the last wait the fetcher will do before returning the response (if enabled). You pass a CSS selector to the `wait_selector` argument, and the fetcher will wait for the state you passed in the `wait_selector_state` argument to be fulfilled. If you didn't pass a state, the default would be `attached`, which means it will wait for the element to be present in the DOM.
|
||||
|
||||
After that, if `load_dom` is enabled (the default), the fetcher will check again to see if all JavaScript files are loaded and executed (in the `domcontentloaded` state) or continue waiting. If you have enabled `network_idle`, the fetcher will wait for `network_idle` to be fulfilled again, as explained above.
|
||||
|
||||
The states the fetcher can wait for can be any of the following ([source](https://playwright.dev/python/docs/api/class-page#page-wait-for-selector)):
|
||||
|
||||
- `attached`: Wait for an element to be present in the DOM.
|
||||
- `detached`: Wait for an element to not be present in the DOM.
|
||||
- `visible`: wait for an element to have a non-empty bounding box and no `visibility:hidden`. Note that an element without any content or with `display:none` has an empty bounding box and is not considered visible.
|
||||
- `hidden`: wait for an element to be either detached from the DOM, or have an empty bounding box, or `visibility:hidden`. This is opposite to the `'visible'` option.
|
||||
|
||||
### Some Stealth Features
|
||||
|
||||
```python
|
||||
page = DynamicFetcher.fetch(
|
||||
'https://example.com',
|
||||
google_search=True,
|
||||
useragent='Mozilla/5.0...', # Custom user agent
|
||||
locale='en-US', # Set browser locale
|
||||
)
|
||||
```
|
||||
|
||||
### General example
|
||||
```python
|
||||
from scrapling.fetchers import DynamicFetcher
|
||||
|
||||
def scrape_dynamic_content():
|
||||
# Use Playwright for JavaScript content
|
||||
page = DynamicFetcher.fetch(
|
||||
'https://example.com/dynamic',
|
||||
network_idle=True,
|
||||
wait_selector='.content'
|
||||
)
|
||||
|
||||
# Extract dynamic content
|
||||
content = page.css('.content')
|
||||
|
||||
return {
|
||||
'title': content.css('h1::text').get(),
|
||||
'items': [
|
||||
item.text for item in content.css('.item')
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
## Session Management
|
||||
|
||||
To keep the browser open until you make multiple requests with the same configuration, use `DynamicSession`/`AsyncDynamicSession` classes. Those classes can accept all the arguments that the `fetch` function can take, which enables you to specify a config for the entire session.
|
||||
|
||||
```python
|
||||
from scrapling.fetchers import DynamicSession
|
||||
|
||||
# Create a session with default configuration
|
||||
with DynamicSession(
|
||||
headless=True,
|
||||
disable_resources=True,
|
||||
real_chrome=True
|
||||
) as session:
|
||||
# Make multiple requests with the same browser instance
|
||||
page1 = session.fetch('https://example1.com')
|
||||
page2 = session.fetch('https://example2.com')
|
||||
page3 = session.fetch('https://dynamic-site.com')
|
||||
|
||||
# All requests reuse the same tab on the same browser instance
|
||||
```
|
||||
|
||||
### Async Session Usage
|
||||
|
||||
```python
|
||||
import asyncio
|
||||
from scrapling.fetchers import AsyncDynamicSession
|
||||
|
||||
async def scrape_multiple_sites():
|
||||
async with AsyncDynamicSession(
|
||||
network_idle=True,
|
||||
timeout=30000,
|
||||
max_pages=3
|
||||
) as session:
|
||||
# Make async requests with shared browser configuration
|
||||
pages = await asyncio.gather(
|
||||
session.fetch('https://spa-app1.com'),
|
||||
session.fetch('https://spa-app2.com'),
|
||||
session.fetch('https://dynamic-content.com')
|
||||
)
|
||||
return pages
|
||||
```
|
||||
|
||||
You may have noticed the `max_pages` argument. This is a new argument that enables the fetcher to create a **rotating pool of Browser tabs**. Instead of using a single tab for all your requests, you set a limit on the maximum number of pages that can be displayed at once. With each request, the library will close all tabs that have finished their task and check if the number of the current tabs is lower than the maximum allowed number of pages/tabs, then:
|
||||
|
||||
1. If you are within the allowed range, the fetcher will create a new tab for you, and then all is as normal.
|
||||
2. Otherwise, it will keep checking every subsecond if creating a new tab is allowed or not for 60 seconds, then raise `TimeoutError`. This can happen when the website you are fetching becomes unresponsive.
|
||||
|
||||
This logic allows for multiple URLs to be fetched at the same time in the same browser, which saves a lot of resources, but most importantly, is so fast :)
|
||||
|
||||
In versions 0.3 and 0.3.1, the pool was reusing finished tabs to save more resources/time. That logic proved flawed, as it's nearly impossible to protect pages/tabs from contamination by the previous configuration used in the request before this one.
|
||||
|
||||
### Session Benefits
|
||||
|
||||
- **Browser reuse**: Much faster subsequent requests by reusing the same browser instance.
|
||||
- **Cookie persistence**: Automatic cookie and session state handling as any browser does automatically.
|
||||
- **Consistent fingerprint**: Same browser fingerprint across all requests.
|
||||
- **Memory efficiency**: Better resource usage compared to launching new browsers with each fetch.
|
||||
|
||||
## When to Use
|
||||
|
||||
Use DynamicFetcher when:
|
||||
|
||||
- Need browser automation
|
||||
- Want multiple browser options
|
||||
- Using a real Chrome browser
|
||||
- Need custom browser config
|
||||
- Want a few stealth options
|
||||
|
||||
If you want more stealth and control without much config, check out the [StealthyFetcher](stealthy.md).
|
||||
@@ -0,0 +1,432 @@
|
||||
# HTTP requests
|
||||
|
||||
The `Fetcher` class provides rapid and lightweight HTTP requests using the high-performance `curl_cffi` library with a lot of stealth capabilities.
|
||||
|
||||
## Basic Usage
|
||||
Import the Fetcher (same import pattern for all fetchers):
|
||||
|
||||
```python
|
||||
>>> from scrapling.fetchers import Fetcher
|
||||
```
|
||||
Check out how to configure the parsing options [here](choosing.md#parser-configuration-in-all-fetchers)
|
||||
|
||||
### Shared arguments
|
||||
All methods for making requests here share some arguments, so let's discuss them first.
|
||||
|
||||
- **url**: The targeted URL
|
||||
- **stealthy_headers**: If enabled (default), it creates and adds real browser headers. It also sets the referer header as if this request came from a Google search of the URL's domain.
|
||||
- **follow_redirects**: As the name implies, tell the fetcher to follow redirections. **Enabled by default**
|
||||
- **timeout**: The number of seconds to wait for each request to be finished. **Defaults to 30 seconds**.
|
||||
- **retries**: The number of retries that the fetcher will do for failed requests. **Defaults to three retries**.
|
||||
- **retry_delay**: Number of seconds to wait between retry attempts. **Defaults to 1 second**.
|
||||
- **impersonate**: Impersonate specific browsers' TLS fingerprints. Accepts browser strings or a list of them like `"chrome110"`, `"firefox102"`, `"safari15_5"` to use specific versions or `"chrome"`, `"firefox"`, `"safari"`, `"edge"` to automatically use the latest version available. This makes your requests appear to come from real browsers at the TLS level. If you pass it a list of strings, it will choose a random one with each request. **Defaults to the latest available Chrome version.**
|
||||
- **http3**: Use HTTP/3 protocol for requests. **Defaults to False**. It might be problematic if used with `impersonate`.
|
||||
- **cookies**: Cookies to use in the request. Can be a dictionary of `name→value` or a list of dictionaries.
|
||||
- **proxy**: As the name implies, the proxy for this request is used to route all traffic (HTTP and HTTPS). The format accepted here is `http://username:password@localhost:8030`.
|
||||
- **proxy_auth**: HTTP basic auth for proxy, tuple of (username, password).
|
||||
- **proxies**: Dict of proxies to use. Format: `{"http": proxy_url, "https": proxy_url}`.
|
||||
- **proxy_rotator**: A `ProxyRotator` instance for automatic proxy rotation. Cannot be combined with `proxy` or `proxies`.
|
||||
- **headers**: Headers to include in the request. Can override any header generated by the `stealthy_headers` argument
|
||||
- **max_redirects**: Maximum number of redirects. **Defaults to 30**, use -1 for unlimited.
|
||||
- **verify**: Whether to verify HTTPS certificates. **Defaults to True**.
|
||||
- **cert**: Tuple of (cert, key) filenames for the client certificate.
|
||||
- **selector_config**: A dictionary of custom parsing arguments to be used when creating the final `Selector`/`Response` class.
|
||||
|
||||
**Notes:**
|
||||
1. The currently available browsers to impersonate are (`"edge"`, `"chrome"`, `"chrome_android"`, `"safari"`, `"safari_beta"`, `"safari_ios"`, `"safari_ios_beta"`, `"firefox"`, `"tor"`)
|
||||
2. The available browsers to impersonate, along with their corresponding versions, are automatically displayed in the argument autocompletion and updated with each `curl_cffi` update.
|
||||
3. If any of the arguments `impersonate` or `stealthy_headers` are enabled, the fetchers will automatically generate real browser headers that match the browser version used.
|
||||
|
||||
Other than this, for further customization, you can pass any arguments that `curl_cffi` supports for any method if that method doesn't already support them.
|
||||
|
||||
### HTTP Methods
|
||||
There are additional arguments for each method, depending on the method, such as `params` for GET requests and `data`/`json` for POST/PUT/DELETE requests.
|
||||
|
||||
Examples are the best way to explain this:
|
||||
|
||||
> Hence: `OPTIONS` and `HEAD` methods are not supported.
|
||||
#### GET
|
||||
```python
|
||||
>>> from scrapling.fetchers import Fetcher
|
||||
>>> # Basic GET
|
||||
>>> page = Fetcher.get('https://example.com')
|
||||
>>> page = Fetcher.get('https://scrapling.requestcatcher.com/get', stealthy_headers=True, follow_redirects=True)
|
||||
>>> page = Fetcher.get('https://scrapling.requestcatcher.com/get', proxy='http://username:password@localhost:8030')
|
||||
>>> # With parameters
|
||||
>>> page = Fetcher.get('https://example.com/search', params={'q': 'query'})
|
||||
>>>
|
||||
>>> # With headers
|
||||
>>> page = Fetcher.get('https://example.com', headers={'User-Agent': 'Custom/1.0'})
|
||||
>>> # Basic HTTP authentication
|
||||
>>> page = Fetcher.get("https://example.com", auth=("my_user", "password123"))
|
||||
>>> # Browser impersonation
|
||||
>>> page = Fetcher.get('https://example.com', impersonate='chrome')
|
||||
>>> # HTTP/3 support
|
||||
>>> page = Fetcher.get('https://example.com', http3=True)
|
||||
```
|
||||
And for asynchronous requests, it's a small adjustment
|
||||
```python
|
||||
>>> from scrapling.fetchers import AsyncFetcher
|
||||
>>> # Basic GET
|
||||
>>> page = await AsyncFetcher.get('https://example.com')
|
||||
>>> page = await AsyncFetcher.get('https://scrapling.requestcatcher.com/get', stealthy_headers=True, follow_redirects=True)
|
||||
>>> page = await AsyncFetcher.get('https://scrapling.requestcatcher.com/get', proxy='http://username:password@localhost:8030')
|
||||
>>> # With parameters
|
||||
>>> page = await AsyncFetcher.get('https://example.com/search', params={'q': 'query'})
|
||||
>>>
|
||||
>>> # With headers
|
||||
>>> page = await AsyncFetcher.get('https://example.com', headers={'User-Agent': 'Custom/1.0'})
|
||||
>>> # Basic HTTP authentication
|
||||
>>> page = await AsyncFetcher.get("https://example.com", auth=("my_user", "password123"))
|
||||
>>> # Browser impersonation
|
||||
>>> page = await AsyncFetcher.get('https://example.com', impersonate='chrome110')
|
||||
>>> # HTTP/3 support
|
||||
>>> page = await AsyncFetcher.get('https://example.com', http3=True)
|
||||
```
|
||||
The `page` object in all cases is a [Response](choosing.md#response-object) object, which is a [Selector](parsing/main_classes.md#selector), so you can use it directly
|
||||
```python
|
||||
>>> page.css('.something.something')
|
||||
|
||||
>>> page = Fetcher.get('https://api.github.com/events')
|
||||
>>> page.json()
|
||||
[{'id': '<redacted>',
|
||||
'type': 'PushEvent',
|
||||
'actor': {'id': '<redacted>',
|
||||
'login': '<redacted>',
|
||||
'display_login': '<redacted>',
|
||||
'gravatar_id': '',
|
||||
'url': 'https://api.github.com/users/<redacted>',
|
||||
'avatar_url': 'https://avatars.githubusercontent.com/u/<redacted>'},
|
||||
'repo': {'id': '<redacted>',
|
||||
...
|
||||
```
|
||||
#### POST
|
||||
```python
|
||||
>>> from scrapling.fetchers import Fetcher
|
||||
>>> # Basic POST
|
||||
>>> page = Fetcher.post('https://scrapling.requestcatcher.com/post', data={'key': 'value'}, params={'q': 'query'})
|
||||
>>> page = Fetcher.post('https://scrapling.requestcatcher.com/post', data={'key': 'value'}, stealthy_headers=True, follow_redirects=True)
|
||||
>>> page = Fetcher.post('https://scrapling.requestcatcher.com/post', data={'key': 'value'}, proxy='http://username:password@localhost:8030', impersonate="chrome")
|
||||
>>> # Another example of form-encoded data
|
||||
>>> page = Fetcher.post('https://example.com/submit', data={'username': 'user', 'password': 'pass'}, http3=True)
|
||||
>>> # JSON data
|
||||
>>> page = Fetcher.post('https://example.com/api', json={'key': 'value'})
|
||||
```
|
||||
And for asynchronous requests, it's a small adjustment
|
||||
```python
|
||||
>>> from scrapling.fetchers import AsyncFetcher
|
||||
>>> # Basic POST
|
||||
>>> page = await AsyncFetcher.post('https://scrapling.requestcatcher.com/post', data={'key': 'value'})
|
||||
>>> page = await AsyncFetcher.post('https://scrapling.requestcatcher.com/post', data={'key': 'value'}, stealthy_headers=True, follow_redirects=True)
|
||||
>>> page = await AsyncFetcher.post('https://scrapling.requestcatcher.com/post', data={'key': 'value'}, proxy='http://username:password@localhost:8030', impersonate="chrome")
|
||||
>>> # Another example of form-encoded data
|
||||
>>> page = await AsyncFetcher.post('https://example.com/submit', data={'username': 'user', 'password': 'pass'}, http3=True)
|
||||
>>> # JSON data
|
||||
>>> page = await AsyncFetcher.post('https://example.com/api', json={'key': 'value'})
|
||||
```
|
||||
#### PUT
|
||||
```python
|
||||
>>> from scrapling.fetchers import Fetcher
|
||||
>>> # Basic PUT
|
||||
>>> page = Fetcher.put('https://example.com/update', data={'status': 'updated'})
|
||||
>>> page = Fetcher.put('https://example.com/update', data={'status': 'updated'}, stealthy_headers=True, follow_redirects=True, impersonate="chrome")
|
||||
>>> page = Fetcher.put('https://example.com/update', data={'status': 'updated'}, proxy='http://username:password@localhost:8030')
|
||||
>>> # Another example of form-encoded data
|
||||
>>> page = Fetcher.put("https://scrapling.requestcatcher.com/put", data={'key': ['value1', 'value2']})
|
||||
```
|
||||
And for asynchronous requests, it's a small adjustment
|
||||
```python
|
||||
>>> from scrapling.fetchers import AsyncFetcher
|
||||
>>> # Basic PUT
|
||||
>>> page = await AsyncFetcher.put('https://example.com/update', data={'status': 'updated'})
|
||||
>>> page = await AsyncFetcher.put('https://example.com/update', data={'status': 'updated'}, stealthy_headers=True, follow_redirects=True, impersonate="chrome")
|
||||
>>> page = await AsyncFetcher.put('https://example.com/update', data={'status': 'updated'}, proxy='http://username:password@localhost:8030')
|
||||
>>> # Another example of form-encoded data
|
||||
>>> page = await AsyncFetcher.put("https://scrapling.requestcatcher.com/put", data={'key': ['value1', 'value2']})
|
||||
```
|
||||
|
||||
#### DELETE
|
||||
```python
|
||||
>>> from scrapling.fetchers import Fetcher
|
||||
>>> page = Fetcher.delete('https://example.com/resource/123')
|
||||
>>> page = Fetcher.delete('https://example.com/resource/123', stealthy_headers=True, follow_redirects=True, impersonate="chrome")
|
||||
>>> page = Fetcher.delete('https://example.com/resource/123', proxy='http://username:password@localhost:8030')
|
||||
```
|
||||
And for asynchronous requests, it's a small adjustment
|
||||
```python
|
||||
>>> from scrapling.fetchers import AsyncFetcher
|
||||
>>> page = await AsyncFetcher.delete('https://example.com/resource/123')
|
||||
>>> page = await AsyncFetcher.delete('https://example.com/resource/123', stealthy_headers=True, follow_redirects=True, impersonate="chrome")
|
||||
>>> page = await AsyncFetcher.delete('https://example.com/resource/123', proxy='http://username:password@localhost:8030')
|
||||
```
|
||||
|
||||
## Session Management
|
||||
|
||||
For making multiple requests with the same configuration, use the `FetcherSession` class. It can be used in both synchronous and asynchronous code without issue; the class automatically detects and changes the session type, without requiring a different import.
|
||||
|
||||
The `FetcherSession` class can accept nearly all the arguments that the methods can take, which enables you to specify a config for the entire session and later choose a different config for one of the requests effortlessly, as you will see in the following examples.
|
||||
|
||||
```python
|
||||
from scrapling.fetchers import FetcherSession
|
||||
|
||||
# Create a session with default configuration
|
||||
with FetcherSession(
|
||||
impersonate='chrome',
|
||||
http3=True,
|
||||
stealthy_headers=True,
|
||||
timeout=30,
|
||||
retries=3
|
||||
) as session:
|
||||
# Make multiple requests with the same settings and the same cookies
|
||||
page1 = session.get('https://scrapling.requestcatcher.com/get')
|
||||
page2 = session.post('https://scrapling.requestcatcher.com/post', data={'key': 'value'})
|
||||
page3 = session.get('https://api.github.com/events')
|
||||
|
||||
# All requests share the same session and connection pool
|
||||
```
|
||||
|
||||
You can also use a `ProxyRotator` with `FetcherSession` for automatic proxy rotation across requests:
|
||||
|
||||
```python
|
||||
from scrapling.fetchers import FetcherSession, ProxyRotator
|
||||
|
||||
rotator = ProxyRotator([
|
||||
'http://proxy1:8080',
|
||||
'http://proxy2:8080',
|
||||
'http://proxy3:8080',
|
||||
])
|
||||
|
||||
with FetcherSession(proxy_rotator=rotator, impersonate='chrome') as session:
|
||||
# Each request automatically uses the next proxy in rotation
|
||||
page1 = session.get('https://example.com/page1')
|
||||
page2 = session.get('https://example.com/page2')
|
||||
|
||||
# You can check which proxy was used via the response metadata
|
||||
print(page1.meta['proxy'])
|
||||
```
|
||||
|
||||
You can also override the session proxy (or rotator) for a specific request by passing `proxy=` directly to the request method:
|
||||
|
||||
```python
|
||||
with FetcherSession(proxy='http://default-proxy:8080') as session:
|
||||
# Uses the session proxy
|
||||
page1 = session.get('https://example.com/page1')
|
||||
|
||||
# Override the proxy for this specific request
|
||||
page2 = session.get('https://example.com/page2', proxy='http://special-proxy:9090')
|
||||
```
|
||||
|
||||
And here's an async example
|
||||
|
||||
```python
|
||||
async with FetcherSession(impersonate='firefox', http3=True) as session:
|
||||
# All standard HTTP methods available
|
||||
response = await session.get('https://example.com')
|
||||
response = await session.post('https://scrapling.requestcatcher.com/post', json={'data': 'value'})
|
||||
response = await session.put('https://scrapling.requestcatcher.com/put', data={'update': 'info'})
|
||||
response = await session.delete('https://scrapling.requestcatcher.com/delete')
|
||||
```
|
||||
or better
|
||||
```python
|
||||
import asyncio
|
||||
from scrapling.fetchers import FetcherSession
|
||||
|
||||
# Async session usage
|
||||
async with FetcherSession(impersonate="safari") as session:
|
||||
urls = ['https://example.com/page1', 'https://example.com/page2']
|
||||
|
||||
tasks = [
|
||||
session.get(url) for url in urls
|
||||
]
|
||||
|
||||
pages = await asyncio.gather(*tasks)
|
||||
```
|
||||
|
||||
The `Fetcher` class uses `FetcherSession` to create a temporary session with each request you make.
|
||||
|
||||
### Session Benefits
|
||||
|
||||
- **A lot faster**: 10 times faster than creating a single session for each request
|
||||
- **Cookie persistence**: Automatic cookie handling across requests
|
||||
- **Resource efficiency**: Better memory and CPU usage for multiple requests
|
||||
- **Centralized configuration**: Single place to manage request settings
|
||||
|
||||
## Examples
|
||||
Some well-rounded examples to aid newcomers to Web Scraping
|
||||
|
||||
### Basic HTTP Request
|
||||
|
||||
```python
|
||||
from scrapling.fetchers import Fetcher
|
||||
|
||||
# Make a request
|
||||
page = Fetcher.get('https://example.com')
|
||||
|
||||
# Check the status
|
||||
if page.status == 200:
|
||||
# Extract title
|
||||
title = page.css('title::text').get()
|
||||
print(f"Page title: {title}")
|
||||
|
||||
# Extract all links
|
||||
links = page.css('a::attr(href)').getall()
|
||||
print(f"Found {len(links)} links")
|
||||
```
|
||||
|
||||
### Product Scraping
|
||||
|
||||
```python
|
||||
from scrapling.fetchers import Fetcher
|
||||
|
||||
def scrape_products():
|
||||
page = Fetcher.get('https://example.com/products')
|
||||
|
||||
# Find all product elements
|
||||
products = page.css('.product')
|
||||
|
||||
results = []
|
||||
for product in products:
|
||||
results.append({
|
||||
'title': product.css('.title::text').get(),
|
||||
'price': product.css('.price::text').re_first(r'\d+\.\d{2}'),
|
||||
'description': product.css('.description::text').get(),
|
||||
'in_stock': product.has_class('in-stock')
|
||||
})
|
||||
|
||||
return results
|
||||
```
|
||||
|
||||
### Downloading Files
|
||||
|
||||
```python
|
||||
from scrapling.fetchers import Fetcher
|
||||
|
||||
page = Fetcher.get('https://raw.githubusercontent.com/D4Vinci/Scrapling/main/images/main_cover.png')
|
||||
with open(file='main_cover.png', mode='wb') as f:
|
||||
f.write(page.body)
|
||||
```
|
||||
|
||||
### Pagination Handling
|
||||
|
||||
```python
|
||||
from scrapling.fetchers import Fetcher
|
||||
|
||||
def scrape_all_pages():
|
||||
base_url = 'https://example.com/products?page={}'
|
||||
page_num = 1
|
||||
all_products = []
|
||||
|
||||
while True:
|
||||
# Get current page
|
||||
page = Fetcher.get(base_url.format(page_num))
|
||||
|
||||
# Find products
|
||||
products = page.css('.product')
|
||||
if not products:
|
||||
break
|
||||
|
||||
# Process products
|
||||
for product in products:
|
||||
all_products.append({
|
||||
'name': product.css('.name::text').get(),
|
||||
'price': product.css('.price::text').get()
|
||||
})
|
||||
|
||||
# Next page
|
||||
page_num += 1
|
||||
|
||||
return all_products
|
||||
```
|
||||
|
||||
### Form Submission
|
||||
|
||||
```python
|
||||
from scrapling.fetchers import Fetcher
|
||||
|
||||
# Submit login form
|
||||
response = Fetcher.post(
|
||||
'https://example.com/login',
|
||||
data={
|
||||
'username': 'user@example.com',
|
||||
'password': 'password123'
|
||||
}
|
||||
)
|
||||
|
||||
# Check login success
|
||||
if response.status == 200:
|
||||
# Extract user info
|
||||
user_name = response.css('.user-name::text').get()
|
||||
print(f"Logged in as: {user_name}")
|
||||
```
|
||||
|
||||
### Table Extraction
|
||||
|
||||
```python
|
||||
from scrapling.fetchers import Fetcher
|
||||
|
||||
def extract_table():
|
||||
page = Fetcher.get('https://example.com/data')
|
||||
|
||||
# Find table
|
||||
table = page.css('table')[0]
|
||||
|
||||
# Extract headers
|
||||
headers = [
|
||||
th.text for th in table.css('thead th')
|
||||
]
|
||||
|
||||
# Extract rows
|
||||
rows = []
|
||||
for row in table.css('tbody tr'):
|
||||
cells = [td.text for td in row.css('td')]
|
||||
rows.append(dict(zip(headers, cells)))
|
||||
|
||||
return rows
|
||||
```
|
||||
|
||||
### Navigation Menu
|
||||
|
||||
```python
|
||||
from scrapling.fetchers import Fetcher
|
||||
|
||||
def extract_menu():
|
||||
page = Fetcher.get('https://example.com')
|
||||
|
||||
# Find navigation
|
||||
nav = page.css('nav')[0]
|
||||
|
||||
menu = {}
|
||||
for item in nav.css('li'):
|
||||
links = item.css('a')
|
||||
if links:
|
||||
link = links[0]
|
||||
menu[link.text] = {
|
||||
'url': link['href'],
|
||||
'has_submenu': bool(item.css('.submenu'))
|
||||
}
|
||||
|
||||
return menu
|
||||
```
|
||||
|
||||
## When to Use
|
||||
|
||||
Use `Fetcher` when:
|
||||
|
||||
- Need rapid HTTP requests.
|
||||
- Want minimal overhead.
|
||||
- Don't need JavaScript execution (the website can be scraped through requests).
|
||||
- Need some stealth features (ex, the targeted website is using protection but doesn't use JavaScript challenges).
|
||||
|
||||
Use `FetcherSession` when:
|
||||
|
||||
- Making multiple requests to the same or different sites.
|
||||
- Need to maintain cookies/authentication between requests.
|
||||
- Want connection pooling for better performance.
|
||||
- Require consistent configuration across requests.
|
||||
- Working with APIs that require a session state.
|
||||
|
||||
Use other fetchers when:
|
||||
|
||||
- Need browser automation.
|
||||
- Need advanced anti-bot/stealth capabilities.
|
||||
- Need JavaScript support or interacting with dynamic content
|
||||
@@ -0,0 +1,251 @@
|
||||
# StealthyFetcher
|
||||
|
||||
`StealthyFetcher` is a stealthy browser-based fetcher similar to [DynamicFetcher](dynamic.md), using [Playwright's API](https://playwright.dev/python/docs/intro). It adds advanced anti-bot protection bypass capabilities, most handled automatically. It shares the same browser automation model as `DynamicFetcher`, using [Playwright's Page API](https://playwright.dev/python/docs/api/class-page) for page interaction.
|
||||
|
||||
## Basic Usage
|
||||
You have one primary way to import this Fetcher, which is the same for all fetchers.
|
||||
|
||||
```python
|
||||
>>> from scrapling.fetchers import StealthyFetcher
|
||||
```
|
||||
Check out how to configure the parsing options [here](choosing.md#parser-configuration-in-all-fetchers)
|
||||
|
||||
**Note:** The async version of the `fetch` method is `async_fetch`.
|
||||
|
||||
## What does it do?
|
||||
|
||||
The `StealthyFetcher` class is a stealthy version of the [DynamicFetcher](dynamic.md) class, and here are some of the things it does:
|
||||
|
||||
1. It easily bypasses all types of Cloudflare's Turnstile/Interstitial automatically.
|
||||
2. It bypasses CDP runtime leaks and WebRTC leaks.
|
||||
3. It isolates JS execution, removes many Playwright fingerprints, and stops detection through some of the known behaviors that bots do.
|
||||
4. It generates canvas noise to prevent fingerprinting through canvas.
|
||||
5. It automatically patches known methods to detect running in headless mode and provides an option to defeat timezone mismatch attacks.
|
||||
6. It makes requests look as if they came from Google's search page of the requested website.
|
||||
7. and other anti-protection options...
|
||||
|
||||
## Full list of arguments
|
||||
Scrapling provides many options with this fetcher and its session classes. Before jumping to the [examples](#examples), here's the full list of arguments
|
||||
|
||||
|
||||
| Argument | Description | Optional |
|
||||
|:-------------------:|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|:--------:|
|
||||
| url | Target url | ❌ |
|
||||
| headless | Pass `True` to run the browser in headless/hidden (**default**) or `False` for headful/visible mode. | ✔️ |
|
||||
| disable_resources | Drop requests for unnecessary resources for a speed boost. Requests dropped are of type `font`, `image`, `media`, `beacon`, `object`, `imageset`, `texttrack`, `websocket`, `csp_report`, and `stylesheet`. | ✔️ |
|
||||
| cookies | Set cookies for the next request. | ✔️ |
|
||||
| useragent | Pass a useragent string to be used. **Otherwise, the fetcher will generate and use a real Useragent of the same browser and version.** | ✔️ |
|
||||
| network_idle | Wait for the page until there are no network connections for at least 500 ms. | ✔️ |
|
||||
| load_dom | Enabled by default, wait for all JavaScript on page(s) to fully load and execute (wait for the `domcontentloaded` state). | ✔️ |
|
||||
| timeout | The timeout (milliseconds) used in all operations and waits through the page. The default is 30,000 ms (30 seconds). | ✔️ |
|
||||
| wait | The time (milliseconds) the fetcher will wait after everything finishes before closing the page and returning the `Response` object. | ✔️ |
|
||||
| page_action | Added for automation. Pass a function that takes the `page` object and does the necessary automation. | ✔️ |
|
||||
| wait_selector | Wait for a specific css selector to be in a specific state. | ✔️ |
|
||||
| init_script | An absolute path to a JavaScript file to be executed on page creation for all pages in this session. | ✔️ |
|
||||
| wait_selector_state | Scrapling will wait for the given state to be fulfilled for the selector given with `wait_selector`. _Default state is `attached`._ | ✔️ |
|
||||
| google_search | Enabled by default, Scrapling will set the referer header as if this request came from a Google search of this website's domain name. | ✔️ |
|
||||
| extra_headers | A dictionary of extra headers to add to the request. _The referer set by the `google_search` argument takes priority over the referer set here if used together._ | ✔️ |
|
||||
| proxy | The proxy to be used with requests. It can be a string or a dictionary with only the keys 'server', 'username', and 'password'. | ✔️ |
|
||||
| real_chrome | If you have a Chrome browser installed on your device, enable this, and the Fetcher will launch and use an instance of your browser. | ✔️ |
|
||||
| locale | Specify user locale, for example, `en-GB`, `de-DE`, etc. Locale will affect `navigator.language` value, `Accept-Language` request header value, as well as number and date formatting rules. Defaults to the system default locale. | ✔️ |
|
||||
| timezone_id | Changes the timezone of the browser. Defaults to the system timezone. | ✔️ |
|
||||
| cdp_url | Instead of launching a new browser instance, connect to this CDP URL to control real browsers through CDP. | ✔️ |
|
||||
| user_data_dir | Path to a User Data Directory, which stores browser session data like cookies and local storage. The default is to create a temporary directory. **Only Works with sessions** | ✔️ |
|
||||
| extra_flags | A list of additional browser flags to pass to the browser on launch. | ✔️ |
|
||||
| solve_cloudflare | When enabled, fetcher solves all types of Cloudflare's Turnstile/Interstitial challenges before returning the response to you. | ✔️ |
|
||||
| block_webrtc | Forces WebRTC to respect proxy settings to prevent local IP address leak. | ✔️ |
|
||||
| hide_canvas | Add random noise to canvas operations to prevent fingerprinting. | ✔️ |
|
||||
| allow_webgl | Enabled by default. Disabling it disables WebGL and WebGL 2.0 support entirely. Disabling WebGL is not recommended, as many WAFs now check if WebGL is enabled. | ✔️ |
|
||||
| additional_args | Additional arguments to be passed to Playwright's context as additional settings, and they take higher priority than Scrapling's settings. | ✔️ |
|
||||
| selector_config | A dictionary of custom parsing arguments to be used when creating the final `Selector`/`Response` class. | ✔️ |
|
||||
| blocked_domains | A set of domain names to block requests to. Subdomains are also matched (e.g., `"example.com"` blocks `"sub.example.com"` too). | ✔️ |
|
||||
| proxy_rotator | A `ProxyRotator` instance for automatic proxy rotation. Cannot be combined with `proxy`. | ✔️ |
|
||||
| retries | Number of retry attempts for failed requests. Defaults to 3. | ✔️ |
|
||||
| retry_delay | Seconds to wait between retry attempts. Defaults to 1. | ✔️ |
|
||||
|
||||
In session classes, all these arguments can be set globally for the session. Still, you can configure each request individually by passing some of the arguments here that can be configured on the browser tab level like: `google_search`, `timeout`, `wait`, `page_action`, `extra_headers`, `disable_resources`, `wait_selector`, `wait_selector_state`, `network_idle`, `load_dom`, `solve_cloudflare`, `blocked_domains`, `proxy`, and `selector_config`.
|
||||
|
||||
**Notes:**
|
||||
|
||||
1. It's basically the same arguments as [DynamicFetcher](dynamic.md) class, but with these additional arguments: `solve_cloudflare`, `block_webrtc`, `hide_canvas`, and `allow_webgl`.
|
||||
2. The `disable_resources` option made requests ~25% faster in tests for some websites and can help save proxy usage, but be careful with it, as it can cause some websites to never finish loading.
|
||||
3. The `google_search` argument is enabled by default for all requests, making the request appear to come from a Google search page. So, a request for `https://example.com` will set the referer to `https://www.google.com/search?q=example`. Also, if used together, it takes priority over the referer set by the `extra_headers` argument.
|
||||
4. If you didn't set a user agent and enabled headless mode, the fetcher will generate a real user agent for the same browser version and use it. If you didn't set a user agent and didn't enable headless mode, the fetcher will use the browser's default user agent, which is the same as in standard browsers in the latest versions.
|
||||
|
||||
## Examples
|
||||
|
||||
### Cloudflare and stealth options
|
||||
|
||||
```python
|
||||
# Automatic Cloudflare solver
|
||||
page = StealthyFetcher.fetch('https://nopecha.com/demo/cloudflare', solve_cloudflare=True)
|
||||
|
||||
# Works with other stealth options
|
||||
page = StealthyFetcher.fetch(
|
||||
'https://protected-site.com',
|
||||
solve_cloudflare=True,
|
||||
block_webrtc=True,
|
||||
real_chrome=True,
|
||||
hide_canvas=True,
|
||||
google_search=True,
|
||||
proxy='http://username:password@host:port', # It can also be a dictionary with only the keys 'server', 'username', and 'password'.
|
||||
)
|
||||
```
|
||||
|
||||
The `solve_cloudflare` parameter enables automatic detection and solving all types of Cloudflare's Turnstile/Interstitial challenges:
|
||||
|
||||
- JavaScript challenges (managed)
|
||||
- Interactive challenges (clicking verification boxes)
|
||||
- Invisible challenges (automatic background verification)
|
||||
|
||||
And even solves the custom pages with embedded captcha.
|
||||
|
||||
**Important notes:**
|
||||
|
||||
1. Sometimes, with websites that use custom implementations, you will need to use `wait_selector` to make sure Scrapling waits for the real website content to be loaded after solving the captcha. Some websites can be the real definition of an edge case while we are trying to make the solver as generic as possible.
|
||||
2. The timeout should be at least 60 seconds when using the Cloudflare solver for sufficient challenge-solving time.
|
||||
3. This feature works seamlessly with proxies and other stealth options.
|
||||
|
||||
### Browser Automation
|
||||
This is where your knowledge about [Playwright's Page API](https://playwright.dev/python/docs/api/class-page) comes into play. The function you pass here takes the page object from Playwright's API, performs the desired action, and then the fetcher continues.
|
||||
|
||||
This function is executed immediately after waiting for `network_idle` (if enabled) and before waiting for the `wait_selector` argument, allowing it to be used for purposes beyond automation. You can alter the page as you want.
|
||||
|
||||
In the example below, I used the pages' [mouse events](https://playwright.dev/python/docs/api/class-mouse) to scroll the page with the mouse wheel, then move the mouse.
|
||||
```python
|
||||
from playwright.sync_api import Page
|
||||
|
||||
def scroll_page(page: Page):
|
||||
page.mouse.wheel(10, 0)
|
||||
page.mouse.move(100, 400)
|
||||
page.mouse.up()
|
||||
|
||||
page = StealthyFetcher.fetch('https://example.com', page_action=scroll_page)
|
||||
```
|
||||
Of course, if you use the async fetch version, the function must also be async.
|
||||
```python
|
||||
from playwright.async_api import Page
|
||||
|
||||
async def scroll_page(page: Page):
|
||||
await page.mouse.wheel(10, 0)
|
||||
await page.mouse.move(100, 400)
|
||||
await page.mouse.up()
|
||||
|
||||
page = await StealthyFetcher.async_fetch('https://example.com', page_action=scroll_page)
|
||||
```
|
||||
|
||||
### Wait Conditions
|
||||
```python
|
||||
# Wait for the selector
|
||||
page = StealthyFetcher.fetch(
|
||||
'https://example.com',
|
||||
wait_selector='h1',
|
||||
wait_selector_state='visible'
|
||||
)
|
||||
```
|
||||
This is the last wait the fetcher will do before returning the response (if enabled). You pass a CSS selector to the `wait_selector` argument, and the fetcher will wait for the state you passed in the `wait_selector_state` argument to be fulfilled. If you didn't pass a state, the default would be `attached`, which means it will wait for the element to be present in the DOM.
|
||||
|
||||
After that, if `load_dom` is enabled (the default), the fetcher will check again to see if all JavaScript files are loaded and executed (in the `domcontentloaded` state) or continue waiting. If you have enabled `network_idle`, the fetcher will wait for `network_idle` to be fulfilled again, as explained above.
|
||||
|
||||
The states the fetcher can wait for can be any of the following ([source](https://playwright.dev/python/docs/api/class-page#page-wait-for-selector)):
|
||||
|
||||
- `attached`: Wait for an element to be present in the DOM.
|
||||
- `detached`: Wait for an element to not be present in the DOM.
|
||||
- `visible`: wait for an element to have a non-empty bounding box and no `visibility:hidden`. Note that an element without any content or with `display:none` has an empty bounding box and is not considered visible.
|
||||
- `hidden`: wait for an element to be either detached from the DOM, or have an empty bounding box, or `visibility:hidden`. This is opposite to the `'visible'` option.
|
||||
|
||||
|
||||
### Real-world example (Amazon)
|
||||
This is for educational purposes only; this example was generated by AI, which also shows how easy it is to work with Scrapling through AI
|
||||
```python
|
||||
def scrape_amazon_product(url):
|
||||
# Use StealthyFetcher to bypass protection
|
||||
page = StealthyFetcher.fetch(url)
|
||||
|
||||
# Extract product details
|
||||
return {
|
||||
'title': page.css('#productTitle::text').get().clean(),
|
||||
'price': page.css('.a-price .a-offscreen::text').get(),
|
||||
'rating': page.css('[data-feature-name="averageCustomerReviews"] .a-popover-trigger .a-color-base::text').get(),
|
||||
'reviews_count': page.css('#acrCustomerReviewText::text').re_first(r'[\d,]+'),
|
||||
'features': [
|
||||
li.get().clean() for li in page.css('#feature-bullets li span::text')
|
||||
],
|
||||
'availability': page.css('#availability')[0].get_all_text(strip=True),
|
||||
'images': [
|
||||
img.attrib['src'] for img in page.css('#altImages img')
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
## Session Management
|
||||
|
||||
To keep the browser open until you make multiple requests with the same configuration, use `StealthySession`/`AsyncStealthySession` classes. Those classes can accept all the arguments that the `fetch` function can take, which enables you to specify a config for the entire session.
|
||||
|
||||
```python
|
||||
from scrapling.fetchers import StealthySession
|
||||
|
||||
# Create a session with default configuration
|
||||
with StealthySession(
|
||||
headless=True,
|
||||
real_chrome=True,
|
||||
block_webrtc=True,
|
||||
solve_cloudflare=True
|
||||
) as session:
|
||||
# Make multiple requests with the same browser instance
|
||||
page1 = session.fetch('https://example1.com')
|
||||
page2 = session.fetch('https://example2.com')
|
||||
page3 = session.fetch('https://nopecha.com/demo/cloudflare')
|
||||
|
||||
# All requests reuse the same tab on the same browser instance
|
||||
```
|
||||
|
||||
### Async Session Usage
|
||||
|
||||
```python
|
||||
import asyncio
|
||||
from scrapling.fetchers import AsyncStealthySession
|
||||
|
||||
async def scrape_multiple_sites():
|
||||
async with AsyncStealthySession(
|
||||
real_chrome=True,
|
||||
block_webrtc=True,
|
||||
solve_cloudflare=True,
|
||||
timeout=60000, # 60 seconds for Cloudflare challenges
|
||||
max_pages=3
|
||||
) as session:
|
||||
# Make async requests with shared browser configuration
|
||||
pages = await asyncio.gather(
|
||||
session.fetch('https://site1.com'),
|
||||
session.fetch('https://site2.com'),
|
||||
session.fetch('https://protected-site.com')
|
||||
)
|
||||
return pages
|
||||
```
|
||||
|
||||
You may have noticed the `max_pages` argument. This is a new argument that enables the fetcher to create a **rotating pool of Browser tabs**. Instead of using a single tab for all your requests, you set a limit on the maximum number of pages that can be displayed at once. With each request, the library will close all tabs that have finished their task and check if the number of the current tabs is lower than the maximum allowed number of pages/tabs, then:
|
||||
|
||||
1. If you are within the allowed range, the fetcher will create a new tab for you, and then all is as normal.
|
||||
2. Otherwise, it will keep checking every subsecond if creating a new tab is allowed or not for 60 seconds, then raise `TimeoutError`. This can happen when the website you are fetching becomes unresponsive.
|
||||
|
||||
This logic allows for multiple URLs to be fetched at the same time in the same browser, which saves a lot of resources, but most importantly, is so fast :)
|
||||
|
||||
In versions 0.3 and 0.3.1, the pool was reusing finished tabs to save more resources/time. That logic proved flawed, as it's nearly impossible to protect pages/tabs from contamination by the previous configuration used in the request before this one.
|
||||
|
||||
### Session Benefits
|
||||
|
||||
- **Browser reuse**: Much faster subsequent requests by reusing the same browser instance.
|
||||
- **Cookie persistence**: Automatic cookie and session state handling as any browser does automatically.
|
||||
- **Consistent fingerprint**: Same browser fingerprint across all requests.
|
||||
- **Memory efficiency**: Better resource usage compared to launching new browsers with each fetch.
|
||||
|
||||
## When to Use
|
||||
|
||||
Use StealthyFetcher when:
|
||||
|
||||
- Bypassing anti-bot protection
|
||||
- Need a reliable browser fingerprint
|
||||
- Full JavaScript support needed
|
||||
- Want automatic stealth features
|
||||
- Need browser automation
|
||||
- Dealing with Cloudflare protection
|
||||
Reference in New Issue
Block a user