如何动态更换或隐藏IP?基于Python3+Selenium ChromeDriver的Windows环境
Hey there! I’ve dealt with this exact IP blocking headache when using Selenium with ChromeDriver on Windows—nothing kills a scraping workflow faster than getting blocked mid-job. Let’s walk through practical, actionable ways to rotate or hide your IP, plus some extra tricks to avoid detection entirely.
The simplest starting point is using a proxy server to route your Chrome traffic through a different IP. Here’s how to set it up for Windows:
First, grab a working HTTP/HTTPS proxy (free ones work for testing, but residential proxies are way more reliable for long-term scraping). Then configure ChromeOptions to use it:
from selenium import webdriver from selenium.webdriver.chrome.options import Options chrome_options = Options() # Replace with your proxy details: IP:PORT proxy = "123.45.67.89:8080" chrome_options.add_argument(f'--proxy-server=http://{proxy}') # Optional: Add authentication if your proxy requires it # chrome_options.add_argument('--proxy-auth=username:password') driver = webdriver.Chrome(options=chrome_options) driver.get("https://www.example.com")
Pro tip: Free proxies are often slow or get blocked quickly—invest in a paid residential proxy service if you’re doing serious scraping.
Static proxies only get you so far. To avoid being flagged, you need to switch proxies regularly. Here’s a quick way to implement a rotating proxy pool:
First, create a list of working proxies (you can scrape public lists or use a service that provides fresh ones). Then randomly pick a proxy for each new driver instance:
import random from selenium import webdriver from selenium.webdriver.chrome.options import Options # Your proxy pool (replace with real working proxies) proxy_list = [ "111.222.333.444:8080", "555.666.777.888:3128", "999.000.111.222:8888" ] def get_random_proxy(): return random.choice(proxy_list) # Set up driver with random proxy chrome_options = Options() proxy = get_random_proxy() chrome_options.add_argument(f'--proxy-server=http://{proxy}') driver = webdriver.Chrome(options=chrome_options) driver.get("https://www.example.com")
For production, add logic to test proxies before using them (e.g., send a quick request to check if they’re alive) and remove dead ones from the pool.
If proxies aren’t enough, using a VPN with a command-line interface (CLI) lets you switch IPs with just a few lines of Python. Most major VPNs (like NordVPN, ExpressVPN) have Windows CLIs you can use.
Here’s an example using NordVPN’s CLI (make sure you’ve installed it and logged in first):
import subprocess from selenium import webdriver # Connect to a random NordVPN server subprocess.run(["nordvpn", "connect", "random"], check=True) # Now launch Chrome—traffic will route through the VPN driver = webdriver.Chrome() driver.get("https://www.example.com") # When done, disconnect subprocess.run(["nordvpn", "disconnect"], check=True)
You can extend this to switch servers periodically by disconnecting and reconnecting to a new random server between scraping tasks.
Even with a new IP, Selenium can be detected by sites checking for automation markers. Pair IP rotation with these tweaks to stay under the radar:
import random from selenium import webdriver from selenium.webdriver.chrome.options import Options chrome_options = Options() # Use a random proxy proxy = get_random_proxy() chrome_options.add_argument(f'--proxy-server=http://{proxy}') # Enable headless mode (optional but less detectable than visible windows) chrome_options.add_argument('--headless=new') # Disable automation flags chrome_options.add_argument('--disable-blink-features=AutomationControlled') chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"]) chrome_options.add_experimental_option('useAutomationExtension', False) # Rotate user agents (use a list of real Chrome user agents) user_agents = [ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36" ] chrome_options.add_argument(f'user-agent={random.choice(user_agents)}') driver = webdriver.Chrome(options=chrome_options) # Execute script to remove navigator.webdriver flag driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})") driver.get("https://www.example.com")
I’ve found that combining residential proxies with rotating user agents and disabling automation flags works wonders for avoiding detection on strict sites like e-commerce platforms.
If you need maximum anonymity, you can route Selenium traffic through Tor. First, install Tor Browser on your Windows machine. Then configure Chrome to use Tor’s local proxy (default is 127.0.0.1:9050):
from selenium import webdriver from selenium.webdriver.chrome.options import Options chrome_options = Options() # Tor's SOCKS5 proxy chrome_options.add_argument('--proxy-server=socks5://127.0.0.1:9050') driver = webdriver.Chrome(options=chrome_options) driver.get("https://check.torproject.org/") # Verify you're connected to Tor
Note: Tor is slower than proxies or VPNs, so it’s best for small-scale scraping where anonymity is key.
内容的提问来源于stack exchange,提问作者Dds 03

