
In web crawlers and automated tasks, frequent use of the same IP address may lead to the blocking of the target website. This article will explain how to implement proxy and IP rotation with Python 3 through 3 mainstream solutions, and provide detailed code implementation and pitfall avoidance guide.
proxies_pool = [
    {"http": "http://123.45.67.89:8080", "https": "http://123.45.67.89:8080"},
    {"http": "http://112.233.44.55:3128", "https": "http://112.233.44.55:3128"},
    # Expandable with more proxies...
]
import requests
import random
from time import sleep
def rotate_proxy_request(url):
    while True:
        try:
            proxy = random.choice(proxies_pool)
            response = requests.get(
                url,
                proxies=proxy,
                timeout=10,
                headers={"User-Agent": "Mozilla/5.0"}
            )
            if response.status_code == 200:
                return response.text
        except Exception as e:
            print(f"proxy {proxy} fail: {str(e)}")
            sleep(2)  # Delayed retry after failure
# Usage Examples
data = rotate_proxy_request("https://target-website.com/data")
# middlewares.py
import random
class ProxyMiddleware:
    def process_request(self, request, spider):
        proxy = random.choice(proxies_pool)
        request.meta['proxy'] = proxy['http']
        # Add when authentication is required
        # request.headers['Proxy-Authorization'] = basic_auth_header('user', 'pass')
DOWNLOADER_MIDDLEWARES = {
    'myproject.middlewares.ProxyMiddleware': 543,
}
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
def get_chrome_with_proxy(proxy):
    chrome_options = Options()
    chrome_options.add_argument(f'--proxy-server={proxy}')
    driver = webdriver.Chrome(options=chrome_options)
    return driver
# Usage Examples
driver = get_chrome_with_proxy("123.45.67.89:8080")
driver.get("https://target-site.com")
def validate_proxy(proxy):
    try:
        test = requests.get(
            "http://httpbin.org/ip",
            proxies=proxy,
            timeout=5
        )
        return test.json()['origin'] in proxy['http']
    except:
        return False
| Solution | Applicable | Scenario | Advantages | 
| Requests rotation | Simple crawler | Fast implementation | Self-managed sessions required | 
| Scrapy middleware | Large-scale distributed crawler | Good integration | High learning cost | 
| Selenium automation | JS rendering page | Can simulate real browser | High resource consumption | 
Through the above solutions, developers can choose the appropriate proxy rotation strategy according to specific needs. It is recommended to use paid proxy services in production environments and cooperate with the health check mechanism to ensure the quality of the proxy pool. Pay attention to setting the request interval reasonably and comply with network ethics.
 頂級住宅代理解決方案
頂級住宅代理解決方案 {{item.title}}
                                        {{item.title}}