一、常用基礎設置
from selenium import webdriver # 導入webdriver from auth_proxy import proxyauth_plugin_path options = webdriver.ChromeOptions() #設置谷歌瀏覽器的一些配置選項 options.add_argument('--incognito') #隱身模式(無痕模式) # options.add_argument('--headless') # 無頭模式 options.add_argument('--proxy-server=http://host:port') #設置無賬號密碼代理 # options.add_extension(proxyauth_plugin_path) #設置私密代理
# 屏蔽谷歌瀏覽器正在接收自動化軟件控制提示,加上以下兩行 options.add_experimental_option('useAutomationExtension',False) options.add_experimental_option('excludeSwitches', ['enable-automation']) options.add_argument("disable-blink-features=AutomationControlled")#去掉webdriver痕跡 options.add_argument('--disable-gpu') #規避bug options.add_argument('--no-sandbox') #取消沙盒模式,解決DevToolsActivePort文件不存在的報錯 prefs = {'profile.managed_default_content_settings.images': 2} #不加載圖片, 提升速度 prefs.update({"credentials_enable_service":False,"profile.password_manager_enabled":False}) #登錄時關閉彈出的密碼保存提示框 prefs.update({'profile.default_content_setting_values':{'notifications' :2}}) # 禁用瀏覽器彈窗 options.add_experimental_option('prefs',prefs) driver = webdriver.Chrome(executable_path="xxx/chromedriver.exe", options=options) driver.maximize_window() # 瀏覽器啟動后最大化 # 規避webdriver檢測 driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", { "source": """ Object.defineProperty(navigator,'webdriver',{ get: () => undefined }) """ })
二、用於設置上述私密代理的插件代碼(auth_proxy.py)
from selenium import webdriver def create_proxyauth_extension(proxy_host, proxy_port, proxy_username, proxy_password, scheme='http', plugin_path=None): """Proxy Auth Extension args: proxy_host (str): domain or ip address, ie proxy.domain.com proxy_port (int): port proxy_username (str): auth username proxy_password (str): auth password kwargs: scheme (str): proxy scheme, default http plugin_path (str): absolute path of the extension return str -> plugin_path """ import string import zipfile if plugin_path is None: plugin_path = 'vimm_chrome_proxyauth_plugin.zip' manifest_json = """ { "version": "1.0.0", "manifest_version": 2, "name": "Chrome Proxy", "permissions": [ "proxy", "tabs", "unlimitedStorage", "storage", "<all_urls>", "webRequest", "webRequestBlocking" ], "background": { "scripts": ["background.js"] }, "minimum_chrome_version":"22.0.0" } """ background_js = string.Template( """ var config = { mode: "fixed_servers", rules: { singleProxy: { scheme: "${scheme}", host: "${host}", port: parseInt(${port}) }, bypassList: ["foobar.com"] } }; chrome.proxy.settings.set({value: config, scope: "regular"}, function() {}); function callbackFn(details) { return { authCredentials: { username: "${username}", password: "${password}" } }; } chrome.webRequest.onAuthRequired.addListener( callbackFn, {urls: ["<all_urls>"]}, ['blocking'] ); """ ).substitute( host=proxy_host, port=proxy_port, username=proxy_username, password=proxy_password, scheme=scheme, ) with zipfile.ZipFile(plugin_path, 'w') as zp: zp.writestr("manifest.json", manifest_json) zp.writestr("background.js", background_js) return plugin_path proxyauth_plugin_path = create_proxyauth_extension( proxy_host="http-xxxxxxxx.com", ##代理服務器 proxy_port=端口號, ##代理端口 proxy_username="用戶名", ##認證用戶名 proxy_password="密碼" ##認證密碼 )
注意:代理服務器域名、端口號、用戶名和密碼記得設置