Python Selenium防檢測策略匯總
selenium 防檢測策略的方法匯總
合理設(shè)置延遲:請求間添加隨機(jī)延遲 (2-10秒)
限制爬取頻率:控制每小時(shí)/每天的請求量
輪換用戶代理:準(zhǔn)備至少10個(gè)不同的User-Agent
使用住宅代理:優(yōu)先選擇高質(zhì)量的住宅代理IP
處理驗(yàn)證碼:集成2Captcha或Anti-Captcha服務(wù)
定期更新工具:保持selenium和瀏覽器驅(qū)動最新版本
1. 基礎(chǔ)防檢測配置
from selenium import webdriver from selenium.webdriver.chrome.options import Options def get_stealth_driver(): options = Options() # 基本防檢測設(shè)置 options.add_argument("--disable-blink-features=AutomationControlled") options.add_experimental_option("excludeSwitches", ["enable-automation"]) options.add_experimental_option("useAutomationExtension", False) # 禁用自動化控制標(biāo)志 options.add_argument("--disable-infobars") options.add_argument("--disable-dev-shm-usage") options.add_argument("--no-sandbox") # 隨機(jī)用戶代理 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={random.choice(user_agents)}") driver = webdriver.Chrome(options=options) # 修改navigator.webdriver屬性 driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", { "source": """ Object.defineProperty(navigator, 'webdriver', { get: () => undefined }) """ }) return driver
2. 高級防檢測技術(shù)
2.1 使用 undetected-chromedriver
import undetected_chromedriver as uc ???????def get_undetected_driver(): options = uc.ChromeOptions() # 配置選項(xiàng) options.add_argument("--disable-popup-blocking") options.add_argument("--disable-notifications") # 隨機(jī)窗口大小 import random width = random.randint(1000, 1400) height = random.randint(700, 900) options.add_argument(f"--window-size={width},{height}") driver = uc.Chrome( options=options, version_main=114, # 匹配你的Chrome版本 headless=False, use_subprocess=True ) return driver
2.2 模擬人類行為模式
from selenium.webdriver.common.action_chains import ActionChains import time import random ???????def human_like_behavior(driver, element=None): """模擬人類操作行為""" actions = ActionChains(driver) # 隨機(jī)鼠標(biāo)移動 if element: actions.move_to_element(element) else: x = random.randint(0, 500) y = random.randint(0, 500) actions.move_by_offset(x, y) # 隨機(jī)延遲 time.sleep(random.uniform(0.5, 2.5)) # 隨機(jī)滾動 scroll_amount = random.randint(200, 800) driver.execute_script(f"window.scrollBy(0, {scroll_amount})") time.sleep(random.uniform(0.3, 1.8)) actions.perform()
3. 完整防檢測爬取流程
def stealth_scrape(url): try: # 使用undetected-chromedriver driver = get_undetected_driver() # 訪問目標(biāo)URL driver.get(url) # 隨機(jī)等待 time.sleep(random.uniform(2, 5)) # 模擬人類瀏覽行為 human_like_behavior(driver) # 執(zhí)行實(shí)際爬取操作 # 示例:獲取頁面標(biāo)題 title = driver.title print(f"成功獲取頁面標(biāo)題: {title}") # 更多爬取邏輯... except Exception as e: print(f"爬取過程中發(fā)生錯(cuò)誤: {str(e)}") finally: driver.quit() # 使用示例 stealth_scrape("https://example.com")
4. 額外防護(hù)措施
4.1 代理IP輪換
proxies = [ "123.45.67.89:8080", "98.76.54.32:3128" ] #換成自己的 def get_proxy_driver(): options = uc.ChromeOptions() proxy = random.choice(proxies) options.add_argument(f"--proxy-server=http://{proxy}") return uc.Chrome(options=options)
4.2 指紋混淆
def modify_fingerprint(driver): # 修改屏幕分辨率 driver.execute_script( "Object.defineProperty(screen, 'width', {get: () => 1920});" "Object.defineProperty(screen, 'height', {get: () => 1080});" ) # 修改時(shí)區(qū) driver.execute_cdp_cmd( "Emulation.setTimezoneOverride", {"timezoneId": "America/New_York"} ) # 修改WebGL指紋 driver.execute_script( "const getParameter = WebGLRenderingContext.prototype.getParameter;" "WebGLRenderingContext.prototype.getParameter = function(parameter) {" " if (parameter === 37445) { return 'NVIDIA Corporation'; }" " return getParameter.call(this, parameter);" "};" )
5. 檢測與驗(yàn)證
def test_stealth(driver): test_urls = [ "https://bot.sannysoft.com", "https://arh.antoinevastel.com/bots/areyouheadless" ] for url in test_urls: driver.get(url) time.sleep(3) driver.save_screenshot(f"stealth_test_{url.split('/')[-1]}.png") print(f"測試結(jié)果已保存: stealth_test_{url.split('/')[-1]}.png")
到此這篇關(guān)于Python Selenium防檢測策略匯總的文章就介紹到這了,更多相關(guān)Python Selenium防檢測內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python enumerate函數(shù)遍歷數(shù)據(jù)對象組合過程解析
這篇文章主要介紹了Python enumerate函數(shù)遍歷數(shù)據(jù)對象組合過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12Python轉(zhuǎn)換itertools.chain對象為數(shù)組的方法
這篇文章主要介紹了Python轉(zhuǎn)換itertools.chain對象為數(shù)組的方法,通過代碼給大家介紹了itertools 的 chain() 方法,需要的朋友可以參考下2020-02-02pandas數(shù)據(jù)處理之 標(biāo)簽列字符轉(zhuǎn)數(shù)字的實(shí)現(xiàn)
這篇文章主要介紹了pandas數(shù)據(jù)處理之 標(biāo)簽列字符轉(zhuǎn)數(shù)字的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03基于Python __dict__與dir()的區(qū)別詳解
下面小編就為大家?guī)硪黄赑ython __dict__與dir()的區(qū)別詳解。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-10-10