selenium 设置代理选项
import random
proxy_list = [ '127.0.0.1:proxy:80',
'127.0.0.2:proxy:80']
# proxy_list = [{'http': 'https://14.29.47.90:3128'},]
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
print('--proxy-server={0}'.format(random.choice(proxy_list)))
chrome_options.add_argument('--proxy-server={0}'.format(random.choice(proxy_list)))
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get('https://www.2345.com/')
这样添加选项,执行get后会显示网页无法连接
需要用desired_capabilities添加选项
import random from selenium import webdriver proxy_list = [ '127.0.0.1:proxy:80', '127.0.0.2:proxy:80'] PROXY = '--proxy-server={0}'.format(random.choice(proxy_list)) print(PROXY) chrome_options = webdriver.ChromeOptions() desired_capabilities = chrome_options.to_capabilities() desired_capabilities['proxy'] = { "httpProxy":PROXY, "noProxy":None, "proxyType":"MANUAL", "class":"org.openqa.selenium.Proxy", "autodetect":False } driver = webdriver.Chrome(desired_capabilities=desired_capabilities) driver.get('https://www.2345.com/')