PiratProxies

Web scraping

How to use proxies with aiohttp

aiohttp is the common async HTTP client for Python. The proxy is given per request, which makes it easy to send different requests through different sessions.

Which proxy to use

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.

What to enter

A PiratProxies plan gives you four things: a host, a port, a username and a password. These are the values for Residential; the username and password are on your plan page.

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

As one address:

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

The country and the session are chosen in the username, not in a separate setting. The generator on your plan page writes the username for you, and Targeting and sessions explains each part.

Steps

  1. Pass proxy= with each request.
  2. To use many IP addresses at once, give each task its own session ID in the username.

Example

Python

import asyncio
import aiohttp

async def main():
    async with aiohttp.ClientSession() as session:
        async with session.get("https://api.ipify.org", proxy="http://USERNAME:PASSWORD@resi.piratproxies.com:8080") as r:
            print(await r.text())

asyncio.run(main())

Replace USERNAME and PASSWORD with the login from your plan page. The example asks an IP-check address, so the output is the address your request left from.

Rotating or sticky

For crawling many pages with aiohttp, leave the username without a session so every request leaves from a new IP address. For anything with steps that belong together, such as a login, a cart or a paginated search that sets cookies, add a session to the username so those requests share one address.

A sticky username looks like this:

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

If it does not work

  • aiohttp does not read SOCKS addresses itself. Install aiohttp-socks and use its ProxyConnector for port 1080.
  • Hundreds of requests at once through one username is fine for rotating use. Add a limit with asyncio.Semaphore so the target site is not hammered.
  • A 407 error means the username or password was not accepted. Copy both again from your plan page, and check that nothing added a space or cut the username short.
  • If the IP address is in the wrong country, look at the username: the country code is part of it. More fixes.

Questions

Can aiohttp use SOCKS5 proxies?

Not on its own. Add the aiohttp-socks package and create the session with its ProxyConnector, built from socks5://USERNAME:PASSWORD@resi.piratproxies.com:1080.

Which PiratProxies proxy should I use with aiohttp?

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.

aiohttp is a product of its own makers. PiratProxies is not affiliated with it; the name is used here only to say what this guide is about. Use it within the rules of the sites you work with and our acceptable use policy.