Back to blog

How to Rotate Proxies in Python (Step-by-Step Tutorial)

Eugenijus Denisov

Last updated -
How to

Ready to get started?

Register now

If you’re trying to do web scraping but keep getting blocked, it’s quite common if you don’t follow some best practices. Websites don’t like too many requests from the same IP address.

That’s why smart web scrapers use proxy rotation. In this guide, we will show you how to rotate proxies using Python in two easy ways. Let’s get to it.

What Are the Benefits of Proxy Rotation?

Proxy rotation refers to frequently changing your IP address while online. Basically, using it significantly improves your anonymity, making it harder to trace the traffic back to you. Web servers monitor excessive traffic from a single IP, and if they detect it, they block the connection.

That’s where proxy rotation helps. It keeps you hidden by changing your IP address every time you make a request or at set time intervals. It’s especially beneficial for web scraping big websites, dodging IP bans, and avoiding rate limits. Without it, you’d be shut down almost immediately.

If you scrape without rotating, you’ll hit a wall very fast. Additionally, some sites may even blacklist entire proxy servers if they detect that something wrong is going on.

Tools You’ll Need for Proxy Rotation in Python

To perform proxy rotation in Python, you’ll need a few simple tools:

When you’ve got all of the above sorted, there are two good options:

  • Use a proxy list containing different proxy IP addresses. It’s recommended to avoid using free proxies.
  • Use a residential proxy API, such as IPRoyal’s residential proxies .

If you need some help setting up, check out our guide to web scraping with Python.

Step-by-Step: How to Rotate Proxies in Python

There are two main methods we’ll talk about in this guide: using a proxy list and utilizing a residential rotating proxy API.

Method 1: Using a Proxy List with Requests

First, get your proxy list. While it could technically be a list of free proxies, we strongly advise against it. They may, and most likely will, be troublesome and problematic. And poor proxy performance will almost always equal poor results.

Here’s a simple code snippet for using a proxy list:

import requests
import random
import time

proxy_list = [
    "http://proxy1.example.com:8080",
    "http://proxy2.example.com:8080",
    # Add more proxies here
]

def get_random_proxy():
    return random.choice(proxy_list)

def make_request(url):
    proxy_ip = get_random_proxy()
    proxies = {
        "http": proxy_ip,
        "https": proxy_ip
    }
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)"
    }

    try:
        response = requests.get(url, proxies=proxies, headers=headers, timeout=5)
        return response.text
    except Exception as e:
        print(f"Request failed: {e}")
        return None

print(make_request('http://example.com'))

In this code snippet, we pick a random proxy each time. That’s how you build a small proxy rotator. Ensure that you add some retry logic if necessary. Otherwise, failed requests will leave you hanging.

You can also make a proxy pool to manage many proxies. It’s a great idea if you’re thinking of running heavy web scraping operations. Also, you’ll need to replace the placeholder proxy addresses with your actual proxy addresses.

Lastly, feel free to change the user agent if you have another one in mind.

Method 2: Using a Residential Rotating Proxy API

Using a service like residential proxies is way easier. This service handles the entire proxy rotation and IP rotation automatically.

Here’s a brief example you may try if using IPRoyal:

import requests

proxy = {
    "http": "http://username:[email protected]:12321",
    "https": "http://username:[email protected]:12321",
}

url = "http://example.com"
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)"
}

response = requests.get(url, proxies=proxy, headers=headers)

print(response.text)

This gateway auto-rotates your random proxy IPs behind the scenes. It’s simple, it’s effective, and you can seamlessly integrate it into your existing software. Just don’t forget to update the placeholders in the code, and you’re good to go.

If you’re unsure about the type of proxies you need, you can refer to our guide on static vs rotating proxies .

Conclusion

Learning about proxy rotation is a must if you’re into web scraping. Choosing between a proxy list and a premium service depends on your project needs and scope.

Also, keep in mind that there are many types of proxies available: residential proxies , datacenter proxies , ISP proxies , and mobile proxies .

And while free proxies sound good, all these positive thoughts usually go away when you experience slow speeds, bans, or other common issues. Paid rotating proxy (Python) setups, such as IPRoyal, work much better for serious projects.

FAQ

Is a rotating proxy legal?

Yes, using a proxy rotator for web scraping is generally legal, unless you intentionally violate a site’s terms of service. Proxy rotation with a large proxy pool helps you avoid quick bans and improves web scraping performance.

What’s the difference between sticky and rotating proxies?

Sticky proxies keep the same IP address for longer. Rotating proxies change the IP more often. Web scrapers that use proxy rotation are less likely to get blocked immediately since frequent random proxy selection doesn’t trigger some anti-scraping measures on the website’s side.

Is a proxy safer than a VPN?

For hiding your IP address, both are good. However, VPNs encrypt your data more effectively, and proxies are more useful for scraping. A random proxy rotator minimizes the chances of getting blocked and improves your web scraping efficiency.

Can I rotate proxies without a provider?

Yes, you can make your own proxy list with free proxies. However, the proxy performance during web scraping will most likely be very poor, and you will eventually upgrade to premium proxy rotator services with paid proxy pools.

How do I test my proxy rotation setup?

To test your proxy rotator, send many requests and check if your IP address changes. A good setup will rotate the proxy pool smoothly and without any problems.

Create Account

Author

Eugenijus Denisov

Senior Software Engineer

With over a decade of experience under his belt, Eugenijus has worked on a wide range of projects - from LMS (learning management system) to large-scale custom solutions for businesses and the medical sector. Proficient in PHP, Vue.js, Docker, MySQL, and TypeScript, Eugenijus is dedicated to writing high-quality code while fostering a collaborative team environment and optimizing work processes. Outside of work, you’ll find him running marathons and cycling challenging routes to recharge mentally and build self-confidence.

Learn More About Eugenijus Denisov
Share on

Related articles