Skip to main content

Overview

Scrapy is a great framework for web crawling. This downloader middleware provides a user-agent rotation based on the settings in settings.py, spider, request.

Requirements

  • Tests on Python 2.7 and Python 3.5, but it should work on other version higher then Python 3.3

  • Tests on Linux, but it’s a pure python module, it should work on other platforms with official python supported, e.g. Windows, Mac OSX, BSD

Installation

The quick way:

pip install scrapy-useragents

Or put this middleware just beside the scrapy project.

Documentation

In setting.py, for example:

# -----------------------------------------------------------------------------
# USER AGENT
# -----------------------------------------------------------------------------

DOWNLOADER_MIDDLEWARES.update({
    'scrapy.downloadermiddlewares.useragent.UserAgentMiddleware': None,
    'scrapy_useragents.downloadermiddlewares.useragents.UserAgentsMiddleware': 500,
})

USER_AGENTS = [
    ('Mozilla/5.0 (X11; Linux x86_64) '
     'AppleWebKit/537.36 (KHTML, like Gecko) '
     'Chrome/57.0.2987.110 '
     'Safari/537.36'),  # chrome
    ('Mozilla/5.0 (X11; Linux x86_64) '
     'AppleWebKit/537.36 (KHTML, like Gecko) '
     'Chrome/61.0.3163.79 '
     'Safari/537.36'),  # chrome
    ('Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:55.0) '
     'Gecko/20100101 '
     'Firefox/55.0')  # firefox
]

Settings Reference

USER_AGENTS

A list of User-Agent to use when crawling, unless overridden.

The middleware will rotate this list by function cycle from the module itertools.

Be careful this middleware can’t handle the situation that the COOKIES_ENABLED is True, and the website binds the cookies with User-Agent, it may cause unpredictable result of the spider. This problem will be solved in the future.

TODO

  • Read User-Agent from a backend, e.g. MongoDB, MySQL, or even a file saved on the local disk.

  • Rotate User-Agent binding with cookies, keep the consistence

  • Add meta key for User-Agent selection based on each request

Metadata

Release files for Scrapy-UserAgents 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for Scrapy-UserAgents 0.0.1
File Size Uploaded
Scrapy-UserAgents-0.0.1.tar.gz 4.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for Scrapy-UserAgents 0.0.1
File Interpreter ABI Platform
Scrapy_UserAgents-0.0.1-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 10.3 kB

Release files / Scrapy-UserAgents-0.0.1.tar.gz

Download URL Scrapy-UserAgents-0.0.1.tar.gz
Size 4.5 kB
Tags Source
SHA-256 checksum
How to use checksums
caa6d5b3bdbddcd79678caad3bae5d5cd0f3a96144807acf491925795e75c44e
BLAKE2b-256 checksum
How to use checksums
d753f83dd78f44ad6310aec870f50b216d56f938478b4bdb9886c86aff81bfc4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / Scrapy_UserAgents-0.0.1-py2.py3-none-any.whl

Download URL Scrapy_UserAgents-0.0.1-py2.py3-none-any.whl
Size 5.8 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
316ef88068aa5107591e97c9d04a75effc2600914ac7198bbed52a1c65a4434c
BLAKE2b-256 checksum
How to use checksums
ee37efaea9801d3080facde05b79ece2fe65c0c2265a88ba5d1767432efe6ca9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.0.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page