Selenium 默认不允许使用用户名和密码(API 密钥)进行身份验证。您可以使用下面的变通方法来实现,而不是 IP 白名单。
import osimport zipfilefrom selenium import webdriverPROXY_HOST = 'premium.residential.proxyrack.net' # 这是用于高级住宅代理的。您可以替换为您使用的服务的 DNSPROXY_PORT = 9000 # 端口(使用 "10000" 作为粘性端口)PROXY_USER = 'user' # 您的用户名PROXY_PASS = 'password' # 您的密码(API 密钥)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 = """var config = { mode: "fixed_servers", rules: { singleProxy: { scheme: "http", host: "%s", port: parseInt(%s) }, bypassList: ["localhost"] } };chrome.proxy.settings.set({value: config, scope: "regular"}, function() {});function callbackFn(details) { return { authCredentials: { username: "%s", password: "%s" } };}chrome.webRequest.onAuthRequired.addListener( callbackFn, {urls: ["<all_urls>"]}, ['blocking']);""" % (PROXY_HOST, PROXY_PORT, PROXY_USER, PROXY_PASS)def get_chromedriver(use_proxy=False, user_agent=None): path = os.path.dirname(os.path.abspath(__file__)) chrome_options = webdriver.ChromeOptions() if use_proxy: pluginfile = 'proxy_auth_plugin.zip' with zipfile.ZipFile(pluginfile, 'w') as zp: zp.writestr("manifest.json", manifest_json) zp.writestr("background.js", background_js) chrome_options.add_extension(pluginfile) if user_agent: chrome_options.add_argument('--user-agent=%s' % user_agent) driver = webdriver.Chrome( os.path.join(path, 'chromedriver'), chrome_options=chrome_options) return driverdef main(): driver = get_chromedriver(use_proxy=True) driver.get('http://api.ipify.org/?format=json') # 您想要抓取的任何 URL