For the complete documentation index, see llms.txt. This page is also available as Markdown.

Scrapy

Integrate Scrapy with Oxylabs Proxies to build fast, asynchronous web crawlers using an easy step-by-step guide.

Scrapy is an open-source Python framework designed for large-scale web scraping and data extraction. By default, Scrapy routes requests directly from your local network.

Follow the steps below to integrate the following Oxylabs proxies:

1

Choose integration method

Scrapy supports two primary methods for routing requests through proxies: Request Meta Parameter (per-request configuration) and Custom Downloader Middleware (project-wide configuration).

Method 1: Request Meta Parameter

Pass proxy authentication details directly into individual scrapy.Request instances via the meta dictionary. This method is recommended for simple spiders or when different requests require specific proxy configurations.

import scrapy

class OxylabsSpider(scrapy.Spider):
    name = "oxylabs_spider"
    
    def start_requests(self):
        url = "https://ip.oxylabs.io/"
        proxy = "http://username:password@host:port"
        
        yield scrapy.Request(
            url=url,
            callback=self.parse,
            meta={"proxy": proxy}
        )

    def parse(self, response):
        ip_address = response.css("pre::text").get()
        self.logger.info(f"Connected via Oxylabs Proxy. Active IP: {ip_address}")

To apply proxies automatically across all requests without altering the spider code, implement a custom Scrapy Downloader Middleware.

Open your Scrapy project's middlewares.py and add the OxylabsProxyMiddleware class:

class OxylabsProxyMiddleware:
    @classmethod
    def from_crawler(cls, crawler):
        return cls(crawler.settings)

    def __init__(self, settings):
        self.username = settings.get("OXYLABS_USERNAME")
        self.password = settings.get("OXYLABS_PASSWORD")
        self.endpoint = settings.get("OXYLABS_ENDPOINT")

    def process_request(self, request, spider):
        host = f"http://{self.username}:{self.password}@{self.endpoint}"
        request.meta["proxy"] = host

Then, register the middleware and define your proxy credentials in settings.py:

# Oxylabs Credentials
OXYLABS_USERNAME = "username"
OXYLABS_PASSWORD = "password"
OXYLABS_ENDPOINT = "host:port"

# Enable Downloader Middleware
DOWNLOADER_MIDDLEWARES = {
    "myproject.middlewares.OxylabsProxyMiddleware": 100,
    "scrapy.downloadermiddlewares.httpproxy.HttpProxyMiddleware": 110,
}
2

Configure your proxy

Adjust the proxy details and Oxylabs proxy user’s credentials (created in the Oxylabs dashboard) for your chosen proxy type:

Residential & Mobile Proxies

Host: pr.oxylabs.io

Port: 7777

Username: customer-username

Password: password

You may also use additional parameters with residential and mobile proxies to set the geo-location (country, state, city, and etc.) and control sessions. Check out the Residential Proxies documentation to learn more. The table below provides an overview of available parameters and their example values.

Proxy parameter
Example

Country entry node

Host: us-pr.oxylabs.io

Port: 10000

Country

User: customer-username-cc-US

State

User: customer-username-st-US_texas

City

User: customer-username-city-paris

Continent

User: customer-username-cn-NA

ZIP/Postal code

User: customer-username-cc-US-postalcode-90210

ASN Targeting

User: customer-username-ASN-21928

Session (up to 1440 minutes)

User: customer-username-sessid-qwerty123-sesstime-1440

Sticky session

Host: us-pr.oxylabs.io

Port: 10001

Datacenter Proxies

Host: dc.oxylabs.io

Port: 8001

Username: user-userername

Password: password

The port is the sequential number assigned to an IP address from the proxy list. Thus, port 8001 uses the first IP address from your list. You may also enable proxy IP rotation by setting the port to 8000.

Furthermore, you can specify the geo-location, for example, the US, by setting the Proxy username to user-USERNAME-country-US.

The port 8001 picks a random IP address but stays consistent during a session. Alternatively, you can rotate proxy IPs with each request by using port 8000.

If you want to use a specific geo-location, for example, the US, set the Proxy username to user-USERNAME-country-US.

Dedicated Datacenter Proxies (Enterprise)

Host: 1.2.3.4 (IP address from your proxy list)

Port: 60000 (HTTP) or 1180 (SOCKS5)

Username: username

Password: password

For Enterprise Dedicated Datacenter Proxies (purchased through sales team), you’ll have to choose an IP address from the acquired proxy list.

Dedicated Datacenter Proxies (Self-service)

Host: ddc.oxylabs.io

Port: 8001

Username: user-userername

Password: password

For Self-Service Dedicated Datacenter Proxies (purchased through Oxylabs dashboard), the port indicates the sequential number of an IP address from the acquired proxy list. If you want to get a different IP with each request, change the Port to 8000 to enable proxy rotation.

ISP Proxies

Host: isp.oxylabs.io

Port: 8001

Username: user-username

Password: password

With ISP Proxies, the port is assigned to a specific IP address and its location. Hence, port 8001 will always pick the first IP from your proxy list.

If you want to get a different IP with each request, change the Port to 8000 to enable proxy rotation.

Dedicated ISP Proxies

Host: disp.oxylabs.io

Port: 8001

Username: user-USERNAME

Password: PASSWORD

You will need to choose the port assigned to an IP address from the purchased proxy list, which you can find on the Oxylabs dashboard. The 8001 port will pick the first IP address from your list for all requests. Use port 8000 for automatic proxy IP rotation.

3

Execute the spider

Run your Scrapy spider from the terminal using the crawl command:

scrapy crawl oxylabs_spider

If configured correctly, Scrapy will execute requests through the Oxylabs proxy gateway and log the assigned external exit IP address.

Last updated

Was this helpful?