Skip to main content

PXM2 Scrapy Middleware & Playwright Rotation Helper

License: MIT Python Version Scrapy Compatible Playwright Compatible

Official Downloader Middleware and browser automation helper for integrating PXM2 4G/5G Rotating Mobile Proxies with Scrapy and Playwright.

For an in-depth tutorial and production scraping patterns, read our comprehensive Scrapy Web Scraping Guide.


Features

  • Automatic Proxy Injection: Route all Scrapy requests through PXM2 high-reputation mobile proxy gateways with authentication.
  • Smart Ban Recovery: Detect blocks (403 Forbidden, 429 Too Many Requests, or connection timeouts) and trigger instant modem IP rotation before retrying the request.
  • Frequency-Based Rotation: Rotate cellular IPs automatically every N requests.
  • Hardware Cooldown Handling: Built-in delay allowing cellular modems to complete radio disconnection and obtain a fresh carrier CGNAT IP.
  • Playwright Rotation Helper: Ready-to-use rotator for headless Chromium/Firefox and scrapy-playwright.
  • Zero Heavy Dependencies: Pure standard library implementation.

Installation

pip install pxm2-scrapy

Or install from source:

git clone https://github.com/pxm2/pxm2-scrapy-middleware.git
cd pxm2-scrapy-middleware
pip install -e .

Scrapy Setup

1. Enable Middleware in settings.py

Add PXM2ProxyMiddleware to your Scrapy project's DOWNLOADER_MIDDLEWARES and configure your credentials:

# settings.py

DOWNLOADER_MIDDLEWARES = {
    'pxm2_scrapy.PXM2ProxyMiddleware': 610,
}

# PXM2 Mobile Proxy Credentials
PXM2_PROXY_HOST = 'proxy.pxm2.io'
PXM2_PROXY_PORT = 10001
PXM2_PROXY_USERNAME = 'your_username'
PXM2_PROXY_PASSWORD = 'your_password'

# Instant IP rotation webhook URL from your PXM2 dashboard
PXM2_ROTATION_URL = 'https://api.pxm2.io/rotate/YOUR_API_TOKEN'

# HTTP status codes that trigger automatic IP rotation and request retry
PXM2_ROTATE_ON_STATUS = [403, 429, 503]

# Rotate every N requests (0 to disable scheduled rotation)
PXM2_ROTATE_EVERY = 50

# Hardware reconnection cooldown in seconds (default: 5.0)
PXM2_ROTATION_COOLDOWN = 5.0

# Max retries per request after rotation
PXM2_MAX_RETRIES = 3

Scrapy Spider Example

import scrapy

class ProductSpider(scrapy.Spider):
    name = "products"
    start_urls = [
        "https://httpbin.org/ip",
        "https://example.com/products",
    ]

    def parse(self, response):
        self.logger.info("Fetched %s via IP: %s", response.url, response.text.strip())
        # Parse product details...

Playwright Integration

Use PXM2PlaywrightRotator with standalone Playwright or scrapy-playwright:

from playwright.sync_api import sync_playwright
from pxm2_scrapy import PXM2PlaywrightRotator

# Initialize rotator
rotator = PXM2PlaywrightRotator(
    host="proxy.pxm2.io",
    port=10001,
    username="your_username",
    password="your_password",
    rotation_url="https://api.pxm2.io/rotate/YOUR_API_TOKEN",
    cooldown=5.0
)

with sync_playwright() as p:
    browser = p.chromium.launch(
        proxy=rotator.get_proxy_config(),
        headless=True
    )
    context = browser.new_context()
    page = context.new_page()

    page.goto("https://api.ipify.org?format=json")
    print("Initial IP:", page.content())

    # Trigger instant carrier IP rotation
    print("Rotating mobile IP...")
    rotator.rotate()

    page.reload()
    print("New IP after rotation:", page.content())

    browser.close()

Configuration Reference

Setting Type Default Description
PXM2_PROXY_HOST str 'proxy.pxm2.io' Hostname or IP of the PXM2 mobile proxy gateway
PXM2_PROXY_PORT int 10001 Gateway port number
PXM2_PROXY_USERNAME str '' Proxy authentication username
PXM2_PROXY_PASSWORD str '' Proxy authentication password
PXM2_ROTATION_URL str None Webhook URL that triggers hardware modem rotation
PXM2_ROTATE_ON_STATUS list [403, 429] HTTP status codes triggering automatic rotation & retry
PXM2_ROTATE_EVERY int 0 Rotate cellular IP every N requests (0 = disabled)
PXM2_ROTATION_COOLDOWN float 5.0 Cooldown wait time in seconds for modem reconnection
PXM2_MAX_RETRIES int 3 Maximum retry attempts per request when rotated

Why PXM2 4G/5G Mobile Proxies?

Traditional datacenter and residential proxies are increasingly identified and blocked by anti-bot systems (Cloudflare Turnstile, DataDome, PerimeterX, Akamai).

PXM2 Mobile Proxies leverage real SIM modems connected to tier-1 mobile carriers. Because millions of real smartphone users share the same mobile carrier CGNAT IP pools, mobile proxies achieve the highest trust scores and bypass anti-scraping protections effortlessly.

Learn more:


License

MIT License. See LICENSE for details.

Metadata

Release files for pxm2-scrapy 1.0.0

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

Source distribution (sdist)

Source distribution for pxm2-scrapy 1.0.0
File Size Uploaded Provenance Actions
pxm2_scrapy-1.0.0.tar.gz 9.8 kB Without provenance Details for pxm2_scrapy-1.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pxm2-scrapy 1.0.0
File Interpreter ABI Platform Actions
pxm2_scrapy-1.0.0-py3-none-any.whl Python 3 none any Details for pxm2_scrapy-1.0.0-py3-none-any.whl

Total release size: 17.7 kB

Release files / pxm2_scrapy-1.0.0.tar.gz

Download URL pxm2_scrapy-1.0.0.tar.gz
Size 9.8 kB
Tags Source
SHA-256 checksum
How to use checksums
8fff01237d3886318ea90c13828cc56a26839de1649b43369c420241092f0d19
BLAKE2b-256 checksum
How to use checksums
ce97b9f6cae3d24ceae987662153b11e62938c6bddd040a4a49ffc9f12acfc81
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.7

Release files / pxm2_scrapy-1.0.0-py3-none-any.whl

Download URL pxm2_scrapy-1.0.0-py3-none-any.whl
Size 7.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
eee61087c72cf355dd675a1fad9017cc04c3d5c88234e1dcb5eb1c792e61d20e
BLAKE2b-256 checksum
How to use checksums
7cfbad473a380b09c682e346c0c4e6fb7863233e89ab1d7a559ce2e6f7c76bdb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.7

Security for pxm2-scrapy 1.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files No provenance

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