388 lines
15 KiB
Markdown
388 lines
15 KiB
Markdown
# Introduction
|
|
|
|
The `Fetcher` class provides rapid and lightweight HTTP requests using the high-performance `curl_cffi` library with a lot of stealth capabilities.
|
|
|
|
## Basic Usage
|
|
You have one primary way to import this Fetcher, which is the same 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 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 as if they're coming from real browsers at the TLS level. **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}`.
|
|
- **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.
|
|
|
|
> Note: <br/>
|
|
> 1. The currently available browsers to impersonate are (`"edge"`, `"chrome"`, `"chrome_android"`, `"safari"`, `"safari_beta"`, `"safari_ios"`, `"safari_ios_beta"`, `"firefox"`, `"tor"`)<br/>
|
|
> 2. The available browsers to impersonate and their corresponding versions are automatically displayed in the argument autocompletion and updated automatically with each `curl_cffi` update.
|
|
|
|
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 it.
|
|
|
|
### 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, as follows.
|
|
|
|
> 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://httpbin.org/get', stealthy_headers=True, follow_redirects=True)
|
|
>>> page = Fetcher.get('https://httpbin.org/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://httpbin.org/get', stealthy_headers=True, follow_redirects=True)
|
|
>>> page = await AsyncFetcher.get('https://httpbin.org/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)
|
|
```
|
|
Needless to say, the `page` object in all cases is [Response](choosing.md#response-object) object, which is a [Selector](../parsing/main_classes.md#selector) as we said, 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://httpbin.org/post', data={'key': 'value'}, params={'q': 'query'})
|
|
>>> page = Fetcher.post('https://httpbin.org/post', data={'key': 'value'}, stealthy_headers=True, follow_redirects=True)
|
|
>>> page = Fetcher.post('https://httpbin.org/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://httpbin.org/post', data={'key': 'value'})
|
|
>>> page = await AsyncFetcher.post('https://httpbin.org/post', data={'key': 'value'}, stealthy_headers=True, follow_redirects=True)
|
|
>>> page = await AsyncFetcher.post('https://httpbin.org/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://httpbin.org/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://httpbin.org/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 detects and changes the session type automatically 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
|
|
page1 = session.get('https://httpbin.org/get')
|
|
page2 = session.post('https://httpbin.org/post', data={'key': 'value'})
|
|
page3 = session.get('https://api.github.com/events')
|
|
|
|
# All requests share the same session and connection pool
|
|
```
|
|
|
|
And here's an async example
|
|
|
|
```python
|
|
async with FetcherSession(impersonate='firefox', http3=True) as session:
|
|
# All standard HTTP methods available
|
|
response = async session.get('https://example.com')
|
|
response = async session.post('https://httpbin.org/post', json={'data': 'value'})
|
|
response = async session.put('https://httpbin.org/put', data={'update': 'info'})
|
|
response = async session.delete('https://httpbin.org/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_first('title::text')
|
|
print(f"Page title: {title}")
|
|
|
|
# Extract all links
|
|
links = page.css('a::attr(href)')
|
|
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_first('.title::text'),
|
|
'price': product.css_first('.price::text').re_first(r'\d+\.\d{2}'),
|
|
'description': product.css_first('.description::text'),
|
|
'in_stock': product.has_class('in-stock')
|
|
})
|
|
|
|
return results
|
|
```
|
|
|
|
### 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_first('.name::text'),
|
|
'price': product.css_first('.price::text')
|
|
})
|
|
|
|
# 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_first('.user-name::text')
|
|
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_first('table')
|
|
|
|
# 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_first('nav')
|
|
|
|
menu = {}
|
|
for item in nav.css('li'):
|
|
link = item.css_first('a')
|
|
if link:
|
|
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 |