Summary of selenium anti-detection strategy methods
Set delay reasonably: add random delay between requests (2-10 seconds)
Limit crawl frequency: Control the request volume per hour/day
Rotating User Agent: Prepare at least 10 different User-Agents
Use residential agents: Priority to high-quality residential agent IP
Process verification code: Integrate 2Captcha or Anti-Captcha services
Regular update tool: Keep selenium and browser driver latest versions
1. Basic anti-detection configuration
from selenium import webdriver from import Options def get_stealth_driver(): options = Options() # Basic anti-detection settings options.add_argument("--disable-blink-features=AutomationControlled") options.add_experimental_option("excludeSwitches", ["enable-automation"]) options.add_experimental_option("useAutomationExtension", False) # Disable the automation control flag options.add_argument("--disable-infobars") options.add_argument("--disable-dev-shm-usage") options.add_argument("--no-sandbox") # Random User Agent user_agents = [ "Mozilla/5.0 (Windows NT 10.0; Win64; x64)...", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)..." ] import random options.add_argument(f"user-agent={(user_agents)}") driver = (options=options) # Modify properties driver.execute_cdp_cmd("", { "source": """ (navigator, 'webdriver', { get: () => undefined }) """ }) return driver
2. Advanced anti-detection technology
2.1 Using undetected-chromedriver
import undetected_chromedriver as uc def get_undetected_driver(): options = () #Configuration Options options.add_argument("--disable-popup-blocking") options.add_argument("--disable-notifications") # Random window size import random width = (1000, 1400) height = (700, 900) options.add_argument(f"--window-size={width},{height}") driver = ( options=options, version_main=114, # Match your Chrome version headless=False, use_subprocess=True ) return driver
2.2 Simulate human behavior patterns
from .action_chains import ActionChains import time import random def human_like_behavior(driver, element=None): """Simulate human operational behavior""" actions = ActionChains(driver) # Random mouse movement if element: actions.move_to_element(element) else: x = (0, 500) y = (0, 500) actions.move_by_offset(x, y) # Random delay ((0.5, 2.5)) # Random scrolling scroll_amount = (200, 800) driver.execute_script(f"(0, {scroll_amount})") ((0.3, 1.8)) ()
3. Complete anti-detection crawling process
def stealth_scrape(url): try: # Use undetected-chromedriver driver = get_undetected_driver() # Access the target URL (url) # Wait randomly ((2, 5)) # Simulate human browsing behavior human_like_behavior(driver) # Perform actual crawling operations # Example: Get the page title title = print(f"Successfully obtained the page title: {title}") # More crawling logic... except Exception as e: print(f"An error occurred during crawling: {str(e)}") finally: () #User Examplestealth_scrape("")
4. Additional protection measures
4.1 Proxy IP rotation
proxies = [ "123.45.67.89:8080", "98.76.54.32:3128" ] #Switch to your own def get_proxy_driver(): options = () proxy = (proxies) options.add_argument(f"--proxy-server=http://{proxy}") return (options=options)
4.2 Fingerprint confusion
def modify_fingerprint(driver): # Modify screen resolution driver.execute_script( "(screen, 'width', {get: () => 1920});" "(screen, 'height', {get: () => 1080});" ) # Modify the time zone driver.execute_cdp_cmd( "", {"timezoneId": "America/New_York"} ) # Modify WebGL fingerprint driver.execute_script( "const getParameter = ;" " = function(parameter) {" " if (parameter === 37445) { return 'NVIDIA Corporation'; }" " return (this, parameter);" "};" )
5. Detection and verification
def test_stealth(driver): test_urls = [ "", "/bots/areyouheadless" ] for url in test_urls: (url) (3) driver.save_screenshot(f"stealth_test_{('/')[-1]}.png") print(f"Test results have been saved: stealth_test_{('/')[-1]}.png")
This is the end of this article about the summary of Python Selenium anti-detection strategies. For more related Python Selenium anti-detection content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!