Python:Selenium + Chrome添加认证代理
【摘要】 添加无认证代理,以参数形式添加
chromeOptions = webdriver.ChromeOptions()
chromeOptions.add_argument('--proxy-server=http://ip:port')
driver = webdriver.Chrome(chrome_options=chromeOptions)
123
添加认证...
添加无认证代理,以参数形式添加
chromeOptions = webdriver.ChromeOptions()
chromeOptions.add_argument('--proxy-server=http://ip:port')
driver = webdriver.Chrome(chrome_options=chromeOptions)
- 1
- 2
- 3
添加认证代理,以插件的形式添加
1、新建扩展文件夹 Chrome-proxy-helper添加两个文件
Chrome-proxy-helper/
background.js
manifest.json
- 1
- 2
- 3
内容如下
(1)background.js
var config = { mode: "fixed_servers", rules: { singleProxy: { scheme: "http", host: "%proxy_host", port: parseInt(%proxy_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']
);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
(2)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"
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
2、动态生成插件并使用
# -*- coding: utf-8 -*-
import os
import re
import time
import zipfile
from selenium import webdriver
# Chrome代理模板插件(https://github.com/RobinDev/Selenium-Chrome-HTTP-Private-Proxy)目录
CHROME_PROXY_HELPER_DIR = 'Chrome-proxy-helper'
# 存储自定义Chrome代理扩展文件的目录
CUSTOM_CHROME_PROXY_EXTENSIONS_DIR = 'chrome-proxy-extensions'
def get_chrome_proxy_extension(proxy): """ 获取一个Chrome代理扩展,里面配置有指定的代理(带用户名密码认证) proxy - 指定的代理,格式: username:password@ip:port """ m = re.compile('([^:]+):([^\@]+)\@([\d\.]+):(\d+)').search(proxy) if m: # 提取代理的各项参数 username = m.groups()[0] password = m.groups()[1] ip = m.groups()[2] port = m.groups()[3] # 创建一个定制Chrome代理扩展(zip文件) if not os.path.exists(CUSTOM_CHROME_PROXY_EXTENSIONS_DIR): os.mkdir(CUSTOM_CHROME_PROXY_EXTENSIONS_DIR) extension_file_path = os.path.join(CUSTOM_CHROME_PROXY_EXTENSIONS_DIR, '{}.zip'.format(proxy.replace(':', '_'))) if not os.path.exists(extension_file_path): # 扩展文件不存在,创建 zf = zipfile.ZipFile(extension_file_path, mode='w') zf.write(os.path.join(CHROME_PROXY_HELPER_DIR, 'manifest.json'), 'manifest.json') # 替换模板中的代理参数 background_content = open(os.path.join(CHROME_PROXY_HELPER_DIR, 'background.js')).read() background_content = background_content.replace('%proxy_host', ip) background_content = background_content.replace('%proxy_port', port) background_content = background_content.replace('%username', username) background_content = background_content.replace('%password', password) zf.writestr('background.js', background_content) zf.close() return extension_file_path else: raise Exception('Invalid proxy format. Should be username:password@ip:port')
if __name__ == '__main__': # 测试 options = webdriver.ChromeOptions() # 添加一个自定义的代理插件(配置特定的代理,含用户名密码认证) options.add_extension(get_chrome_proxy_extension(proxy="username:password@ip:port")) driver = webdriver.Chrome(options=options) # 访问一个IP回显网站,查看代理配置是否生效了 driver.get('http://httpbin.org/ip') print(driver.page_source) time.sleep(60) driver.close() driver.quit()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
参考:
文章来源: pengshiyu.blog.csdn.net,作者:彭世瑜,版权归原作者所有,如需转载,请联系作者。
原文链接:pengshiyu.blog.csdn.net/article/details/108075369
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)