Skip to main content

scrapy-tor-proxy-rotation

The purpose of this module is to allow rotation of IPs to Scrapy via Tor.

Installation

Simple way to install, via pip:

pip install scrapy-tor-proxy-rotation

Configuring Tor

You need to configure Tor. First, install it:

sudo apt-get install tor

Stop its execution to perform configuration:

sudo service tor stop

Open your configuration file as root, available at /etc/tor/torrc, for example using nano:

sudo nano /etc/tor/torrc

Insert the lines below and save:

ControlPort 9051
CookieAuthentication 0

Restart Tor:

sudo service tor start

You can check your machine's IP and compare it with Tor's by doing the following:

  • To see your machine's IP:
    curl http://icanhazip.com/
    
  • To see Tor's IP:
    torify curl http://icanhazip.com/   
    

Tor proxies are not supported by Scrapy. To get around this problem, it is necessary to use an intermediary, in this case Privoxy.

The Tor proxy server is by default at 127.0.0.1:9050

Installing and configuring Privoxy:

  • Install:
    sudo apt install privoxy
    
  • Stop its execution:
    sudo service privoxy stop
    
  • Configure it to use TOr, open its configuration file:
    sudo nano /etc/privoxy/config
    
  • Add the following lines:
    forward-socks5t / 127.0.0.1:9050 .
    
  • Start it up:
    service privoxy start
    

By default, privoxy will run at the address 127.0.0.1:8118

Test:

torify curl http://icanhazip.com/
curl -x 127.0.0.1:8118 http://icanhazip.com/

The IP shown in the two steps above must be the same.

How to use

After you have made these settings, you can now integrate Tor with Scrapy.

  • Configure the middleware in your project's configuration file (settings.py):

    DOWNLOADER_MIDDLEWARES = {
        ...,
        'scrapy.downloadermiddlewares.httpproxy.HttpProxyMiddleware': 110,
        'tor_ip_rotator.middlewares.TorProxyMiddleware': 100
    }
    
  • Enable the use of extension:

    TOR_IPROTATOR_ENABLED = True
    TOR_IPROTATOR_CHANGE_AFTER = #number of requests made on the same Tor's IP address
    

By default, an IP can be reused after 10 other uses. This value can be changed by the variable TOR_IPROTATOR_ALLOW_REUSE_IP_AFTER, as below:

TOR_IPROTATOR_ALLOW_REUSE_IP_AFTER = 0 #another integer value

A number too large for TOR_IPROTATOR_ALLOW_REUSE_IP_AFTER may make it slower to retrieve a new IP for use or not find one at all. If the value is 0, there will be no record of used IPs.

Release files for scrapy-tor-proxy-rotation 0.0.4

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-tor-proxy-rotation 0.0.4
File Size Uploaded
scrapy-tor-proxy-rotation-0.0.4.tar.gz 5.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for scrapy-tor-proxy-rotation 0.0.4
File Interpreter ABI Platform
scrapy_tor_proxy_rotation-0.0.4-py3-none-any.whl Python 3 none any Details

Total release size: 11.4 kB

Release files / scrapy-tor-proxy-rotation-0.0.4.tar.gz

Download URL scrapy-tor-proxy-rotation-0.0.4.tar.gz
Size 5.7 kB
Tags Source
SHA-256 checksum
How to use checksums
1533a50474afa1d785e3f4b38dabc4849f7dfc8d87f254d55988471ca229bf86
BLAKE2b-256 checksum
How to use checksums
55e688158429d452254e199fae80ce0b5ca74ee21af5e288fdd2f4cd1f03dfb3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.6

Release files / scrapy_tor_proxy_rotation-0.0.4-py3-none-any.whl

Download URL scrapy_tor_proxy_rotation-0.0.4-py3-none-any.whl
Size 5.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
390fb7af52cadca9db38bd1cc96779155cf0805292ab7d514674098be7a1221d
BLAKE2b-256 checksum
How to use checksums
0c3a96053b80375c67a97e0a26fe3766d00e37d965ee87c256b70ee82335fcdd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.6

Release history Release notifications | RSS feed

This release

0.0.4 This release

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

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