Flexible and modern User-Agent rotator middleware for Scrapy, supporting Faker, fake-useragent, and custom providers.
Project description
scrapy-ua-rotator
A modern, pluggable User-Agent rotator middleware for the Scrapy framework.
Supports rotation via:
fake-useragentFaker- Scrapy’s built-in
USER_AGENTsetting
Also supports per-proxy rotation and easy extensibility with custom providers.
📋 Requirements
- Python 3.9+
Faker >= 18.0.0fake-useragent >= 1.5.0
✅ Tested with: Scrapy 2.9, 2.10, 2.11, and 2.12
📦 Installation
pip install scrapy-ua-rotator
⚙️ Configuration
Disable Scrapy’s default middleware and enable ours:
DOWNLOADER_MIDDLEWARES = {
'scrapy.downloadermiddlewares.useragent.UserAgentMiddleware': None,
'scrapy.downloadermiddlewares.retry.RetryMiddleware': None,
'scrapy_ua_rotator.middleware.RandomUserAgentMiddleware': 400,
'scrapy_ua_rotator.middleware.RetryUserAgentMiddleware': 550,
}
Recommended provider order:
FAKEUSERAGENT_PROVIDERS = [
'scrapy_ua_rotator.providers.FakeUserAgentProvider', # Primary provider using the fake-useragent library
'scrapy_ua_rotator.providers.FakerProvider', # Fallback provider that generates synthetic UAs via Faker
'scrapy_ua_rotator.providers.FixedUserAgentProvider', # Final fallback: uses the static USER_AGENT setting
]
# Static user-agent string to be used if all providers fail to return a valid value
USER_AGENT = "Mozilla/5.0 (X11; Linux x86_64)..."
🧩 Provider Details
FakeUserAgentProvider
Assigns a new user-agent using fake-useragent.
Supports fine-tuned filtering via:
FAKE_USERAGENT_RANDOM_UA_TYPE = 'Chrome Mobile iOS' # str; browser to prioritize (default: 'random')
FAKEUSERAGENT_OS = ['Linux'] # str or list[str]; OS filter (default: None — all OSes)
FAKEUSERAGENT_PLATFORMS = ['mobile'] # str or list[str]; platform filter (default: None — all platforms)
FAKEUSERAGENT_FALLBACK = 'Mozilla/5.0 (...)' # str; fallback UA string (default: internal fallback)
💡 Note: See docs for supported options and advanced usage.
FakerProvider
Uses Faker to generate synthetic UA strings.
FAKER_RANDOM_UA_TYPE = 'chrome' # or 'firefox', 'safari', etc.
💡 Note: See docs for supported options and advanced usage.
FixedUserAgentProvider
Simply uses the provided USER_AGENT setting without rotation.
Useful as a fallback if other providers fail.
USER_AGENT = "Mozilla/5.0 ..."
🔀 Proxy-Aware Mode
If you’re using rotating proxies (e.g., via scrapy-proxies), enable per-proxy UA assignment:
RANDOM_UA_PER_PROXY = True
Make sure RandomUserAgentMiddleware has higher priority than your proxy middleware.
🧪 Example Output
To verify it’s working, log your request headers in your spider:
def parse(self, response):
self.logger.info("Using UA: %s", response.request.headers.get('User-Agent'))
🔧 Extending with Custom Providers
Add your own class:
FAKEUSERAGENT_PROVIDERS = [
'your_project.providers.MyCustomProvider',
...
]
Just inherit from BaseProvider and implement get_random_ua().
🤝 Contributing
Contributions, suggestions, and issues are welcome!
Please see CONTRIBUTING.md for guidelines.
📄 License
MIT © Sergei Denisenko
See LICENSE
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file scrapy_ua_rotator-1.0.0.tar.gz.
File metadata
- Download URL: scrapy_ua_rotator-1.0.0.tar.gz
- Upload date:
- Size: 11.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f65195732e69aff7c8d9e81fffe35a3fdaca21187b03579027943a7e48c79ffd
|
|
| MD5 |
68c02ae93dc78340f21f69bb651acbbd
|
|
| BLAKE2b-256 |
612890906d97a10e83e9be27b45635ad8940db6e77f7f63fc25312242e63c7ff
|
File details
Details for the file scrapy_ua_rotator-1.0.0-py3-none-any.whl.
File metadata
- Download URL: scrapy_ua_rotator-1.0.0-py3-none-any.whl
- Upload date:
- Size: 7.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4ca6cb8482e739483d2961be6bb66fb0a9e4020846622a6905d1e157ed63c3e6
|
|
| MD5 |
12891231ddf439ff16ce0ef7fb90e9f4
|
|
| BLAKE2b-256 |
0032190378bf1c315f7e62ebc62f9d4ca3afcd74421a747aebb03632b0cfae6d
|