options = webdriver.ChromeOption()#创建一个驱动
options.add_argument("--headless")
options.add_argument("--disable-gpu")#或许可以解决一些莫名的问题
#options.set_headles()#另外一种方式
不知道为什么,访问某些网站,代理突然就失效了,访问的网页就只返回一行源码,网上寻找了很多也找不到答案。。。
#简单的
options.add_argument("--proxy-server=http://ip:port")#HTTP代理
options.add_argument("--proxy-server=socks5://ip:port")#socks代理
options.add_argument("--proxy-server=ssl://ip:port")#ssl代理
driver = webdriver.Chrome(chrome_option=options)
...
#稍微复杂点的
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
PROXY = "IP:PORT"
desired_capabilities = webdriver.DesiredCapabilities.INTERNETEXPLORER.copy()#创建一个新的驱动
desired_capabilities['proxy'] = {
"httpProxy": PROXY, #http代理
"ftpProxy": PROXY, #ftp代理
"sslProxy": PROXY, #ssl代理
"noProxy": None,
"proxyType": "MANUAL",#代理模式
"class": "org.openqa.selenium.Proxy",
"autodetect": False
}
driver = webdriver.Chrome(chrome_option=options,desired_capabilities=desired_capabilities)
#其实也很简单。。。
autodetect = False
ftpProxy = ”
httpProxy = ”
noProxy = ”
proxyAutoconfigUrl = ”
proxyType = {‘ff_value’: 6, ‘string’: ‘UNSPECIFIED’}
socksPassword = ”
socksProxy = ”
socksUsername = ”
sslProxy =
AUTODETECT = {‘ff_value’: 4, ‘string’: ‘AUTODETECT’}
DIRECT = {‘ff_value’: 0, ‘string’: ‘DIRECT’}
MANUAL = {‘ff_value’: 1, ‘string’: ‘MANUAL’}
PAC = {‘ff_value’: 2, ‘string’: ‘PAC’}
RESERVED_1 = {‘ff_value’: 3, ‘string’: ‘RESERVED1’}
SYSTEM = {‘ff_value’: 5, ‘string’: ‘SYSTEM’}
UNSPECIFIED = {‘ff_value’: 6, ‘string’: ‘UNSPECIFIED’}
options.add_arguments("--ignore-certificate-errors")
options.add_argument("--user-agent=Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.89 Safari/537.36")
prefs = {'profile.default_content_settings.popups': 0, 'download.default_directory': 'd:\\'}#记得是\\转义,0表示默认不通知,直接怼
options.add_experimental_option('prefs',prefs)#是experimental_options