PiratProxies

Web scraping

Proxies in Scrapy: working examples

Scrapy is the main crawling framework for Python. Its built-in proxy middleware reads a proxy value from each request, including a username and password in the address.

The right proxy type for Scrapy

For most scraping, Residential Lite is the cheapest way to get home IP addresses in the right country. Use Residential when you need a particular state or city, and Unlimited Datacenter for targets in the United States that do not block data centre addresses.

The values to enter in Scrapy

Every PiratProxies plan comes down to four values: host, port, username and password. Here they are for Residential; your own username and password are on your plan page.

Hostresi.piratproxies.com
HTTP port8080
SOCKS5 port1080
UsernameUSERNAME
Passwordshown on your plan page

Written as a single address:

http://USERNAME:PASSWORD@resi.piratproxies.com:8080

There is no separate country or session field: both are part of the username. Your plan page has a generator that writes it, and Targeting and sessions describes each piece.

Setting it up in Scrapy

  1. Put the proxy address in meta["proxy"] of each request.
  2. Leave the session out of the username for crawling, so every request gets a new IP address.
  3. For a large project, set the proxy in one downloader middleware instead of in every spider.

Code: Scrapy through a proxy

Spider

import scrapy

class IpSpider(scrapy.Spider):
    name = "ip"

    def start_requests(self):
        yield scrapy.Request("https://api.ipify.org", meta={"proxy": "http://USERNAME:PASSWORD@resi.piratproxies.com:8080"})

    def parse(self, response):
        self.logger.info("Exit IP: %s", response.text)

Put your own login from the plan page in place of USERNAME and PASSWORD. The example calls an IP-check service, so what it prints is the address your request came from.

Should Scrapy rotate IPs or keep one?

When Scrapy is fetching lots of unrelated pages, leave the session out of the username and each request gets a fresh IP address. When requests belong together, such as a login, a basket or paginated results that set cookies, put a session in the username so they all share one address.

Here is what a sticky username looks like:

USERNAME-country-us-time-600-session-a1b2c3d4

When Scrapy will not connect

  • Scrapy has no SOCKS support. Use the HTTP port, 8080.
  • Scrapy keeps connections open, so several requests can leave from one IP address even with a rotating username. That is normal; the address changes when a new connection is opened.
  • Add 407 to RETRY_HTTP_CODES only while debugging. A 407 means the login is wrong, and retrying does not fix it.
  • Error 407 means the login was refused. Copy the username and password from your plan page once more, and make sure no space crept in and the username was not cut short.
  • An IP in the wrong country nearly always comes from the username, since the country code lives there. Other fixes.

Scrapy and proxies: questions

Can Scrapy use a SOCKS5 proxy?

No. Use the HTTP port, 8080, which handles http and https sites alike.

Which PiratProxies plan suits Scrapy best?

For most scraping, Residential Lite is the cheapest way to get home IP addresses in the right country. Use Residential when you need a particular state or city, and Unlimited Datacenter for targets in the United States that do not block data centre addresses.

Words used in this guide: web scraping, rotating proxy, sticky session, rate limiting, headless browser.

Scrapy belongs to its own developers, and PiratProxies has no connection with them; we name it only to say what this guide covers. Follow the rules of the sites you visit and our acceptable use policy.