Skip to main content

Flexible and modern User-Agent rotator middleware for Scrapy, supporting Faker, fake-useragent, and custom providers.

Project description

scrapy-ua-rotator

PyPI Python License Build Status Codecov

A modern, pluggable User-Agent rotator middleware for the Scrapy framework.

Supports rotation via:

Also supports per-proxy rotation and easy extensibility with custom providers.


📋 Requirements

  • Python 3.9+
  • Faker >= 18.0.0
  • fake-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:

USERAGENT_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_UA_TYPE = 'Chrome Mobile iOS'           # str; browser to prioritize (default: 'random')
FAKE_USERAGENT_OS = ['Linux']                          # str or list[str]; OS filter (default: None — all OSes)
FAKE_USERAGENT_PLATFORMS = ['mobile']                  # str or list[str]; platform filter (default: None — all platforms)
FAKE_USERAGENT_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_UA_TYPE = 'chrome'  # or 'firefox', 'safari', etc. (default: 'user_agent' — random web browser)

💡 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:

USERAGENT_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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

scrapy_ua_rotator-1.0.1.tar.gz (11.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

scrapy_ua_rotator-1.0.1-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file scrapy_ua_rotator-1.0.1.tar.gz.

File metadata

  • Download URL: scrapy_ua_rotator-1.0.1.tar.gz
  • Upload date:
  • Size: 11.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for scrapy_ua_rotator-1.0.1.tar.gz
Algorithm Hash digest
SHA256 32701c60190bc565be3cfcd2cb439cc40c15a7a6833165160e7e4ba90fab749f
MD5 87daa2ed052cb8989ba0c9a72bb4c6a0
BLAKE2b-256 ccbb03b781cbf4705d2a43f3a2f58ee079b1dd8ac9c593a60929b46c25ded649

See more details on using hashes here.

File details

Details for the file scrapy_ua_rotator-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for scrapy_ua_rotator-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ff985f2b92736ecc3af0769bb276a51576738648a5bc8e700d697c06c30b7a49
MD5 c384136e69926f38a502b2b713c79054
BLAKE2b-256 8ad58c33068c44c8ca3c3d8d0f9c6c9f813c0bcc5a6b79d62ad665e5f2eb42c4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page