1. 程式人生 > >selenium 設置代理選項

selenium 設置代理選項

color code www. desire option nop port false print

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/
)

selenium 設置代理選項