selenium实现ip代理http认证
selenium实现ip代理http认证
- 原理
selenium本身支持的ip不支持用户认证,想要认证用户代理ip, 本文的思想就是给浏览器装插件,也就是代理插件。
其中插件的包来源于https://github.com/RobinDev/S...
注意:文件路径自己修改 - 代码如下:
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 = '../cookies/Chrome-proxy-helper' # 存储自定义Chrome代理扩展文件的目录 CUSTOM_CHROME_PROXY_EXTENSIONS_DIR = '../cookies/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='cj422w:[email protected]:888')) print(options) driver = webdriver.Chrome(chrome_options=options) # 访问一个IP回显网站,查看代理配置是否生效了 driver.get('http://httpbin.org/ip') print(driver.page_source) time.sleep(60) driver.quit()
相关推荐
yanghui0 2020-06-20
xiangxiaojun 2020-09-23
Reiki 2020-08-16
letheashura 2020-08-14
tiankele0 2020-07-29
curiousL 2020-07-18
王练 2020-07-18
tiankele0 2020-07-18
amei0 2020-07-08
Reiki 2020-07-06
Ronnyxie 2020-07-06
xiangxiaojun 2020-07-05
zhanghaibing00 2020-06-28
xiongyouqiang 2020-06-28
Ronnyxie 2020-06-27
amei0 2020-06-26
letheashura 2020-06-26
tiankele0 2020-06-26