python配置有权限代理解决方案
代理配置:
proxy_url = "http://{}:{}@{}:{}/".format(proxyUser, proxyPass, proxyHost, proxyPort) proxies = {'https': proxy_url, 'http': proxy_url}
1. requests
response = requests.get(url, proxies=proxies)
2. pdfkit
pdfkit.from_url(url, "filename.pdf", options={"bypass-proxy-for": proxy_url})
3. zeep
client = Client(BI_WDSL_URL) client.transport.session.proxies = proxies
4. selenium + chrome
def create_proxy_auth_extension(proxy_host, proxy_port, proxy_username, proxy_password, scheme='http', plugin_path=None): if plugin_path is None: # default_plugin_path为任意可访问路径 plugin_path = r'{}/{}_{}@http-pro.abuyun.com_9010.zip'.format(default_plugin_path, proxy_username, proxy_password) manifest_json = """ { "version": "1.0.0", "manifest_version": 2, "name": "Abuyun 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 def get_driver(): app.logger.info("open the brower") options = webdriver.ChromeOptions() options.add_argument("start-maximized") options.add_argument("enable-automation") options.add_argument("--no-sandbox") options.add_argument("--disable-infobars") options.add_argument("--disable-dev-shm-usage") options.add_argument("--disable-browser-side-navigation") options.add_argument("--disable-gpu") if proxyUsed: proxy_auth_plugin_path = create_proxy_auth_extension( proxy_host=proxyHost, proxy_port=proxyPort, proxy_username=proxyUser, proxy_password=proxyPass) options.add_extension(proxy_auth_plugin_path) else: options.add_argument("--headless") driver = webdriver.Chrome(chrome_options=options) return driver
Ps. 代理有权限验证时Chrome必须有图像化界面