能否用Python Selenium WebDriver设置代理链,结合Tor与BrowserMob-Proxy改指纹?
Answers to Your Selenium Proxy & Fingerprint Questions
1. Can we set up a proxy chain with Python Selenium WebDriver?
Absolutely! While Selenium doesn’t natively support chaining multiple proxies directly, you can pull this off using an intermediate proxy server (like BrowserMob-Proxy or mitmproxy) as a local hub to route traffic through your desired chain.
Here’s the core logic:
- Configure Selenium to send all traffic to the intermediate local proxy.
- Set up that intermediate proxy to forward traffic to your next proxy (e.g., Tor, another HTTP proxy).
- You can even extend this chain further by adding more intermediate proxies if your use case requires it.
2. Combining Tor with BrowserMob-Proxy to modify request headers & browser fingerprints
Great question — this combination works perfectly for your testing needs! BrowserMob-Proxy (BMP) lets you intercept and tweak HTTP request headers, while Tor handles anonymizing your traffic. Here’s a practical step-by-step guide to implement this:
Prerequisites
- Install required packages:
pip install browsermob-proxy selenium - Ensure Tor is running locally (default Socks5 port is
9050for the standalone Tor service,9150for Tor Browser). - Download the BrowserMob-Proxy binary matching your operating system.
Step-by-Step Implementation Code
from browsermobproxy import Server from selenium import webdriver from selenium.webdriver.chrome.options import Options # 1. Start BrowserMob-Proxy (replace with your BMP binary path) server = Server("/path/to/your/browsermob-proxy") server.start() proxy = server.create_proxy() # 2. Configure BMP to route traffic through Tor's Socks5 proxy proxy.set_proxy_settings({ "socksProxy": "127.0.0.1:9050", # Update to 9150 if using Tor Browser "socksVersion": 5 }) # 3. Set up Chrome options for Selenium chrome_options = Options() # Use BMP as the proxy for Chrome chrome_options.add_argument(f'--proxy-server={proxy.proxy}') # Additional fingerprint modifications (beyond request headers) chrome_options.add_argument("--disable-webrtc") # Prevent IP leaks via WebRTC chrome_options.add_argument("--disable-blink-features=AutomationControlled") # Avoid basic bot detection chrome_options.add_argument("user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36") # Custom user-agent # 4. Launch the Chrome browser driver = webdriver.Chrome(options=chrome_options) # 5. Use BMP to modify HTTP request headers proxy.headers({ "Accept-Language": "fr-FR,fr;q=0.9,en;q=0.8", "Referer": "https://google.com", "X-Requested-With": "XMLHttpRequest" }) # 6. Test your setup driver.get("https://example.com") print("Page title:", driver.title) # Cleanup resources when done driver.quit() proxy.close() server.stop()
Key Notes
- Fingerprint Beyond Headers: BrowserMob-Proxy focuses on HTTP request/response changes. To fully alter browser fingerprints (like canvas, WebGL, or font rendering), consider using
undetected-chromedriverinstead of the standard Selenium ChromeDriver — it’s built to bypass bot detection and mask automation traces. - Tor Configuration: Make sure Tor is running before executing the script. If using Tor Browser, enable "Allow localhost connections" in its preferences to let BMP route traffic through it.
- BMP Compatibility: Ensure your BrowserMob-Proxy version matches your Chrome/Firefox version — older BMP builds might not work with newer browsers.
内容的提问来源于stack exchange,提问作者Shaon Majumder
相关产品推荐
相关产品推荐

