python+selenium设置chrome代理的方式
方式1:
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.proxy import Proxy, ProxyType
chrome_options = webdriver.ChromeOptions()
proxy_ip = '10.168.162.95:8080' # 代理地址和端口
chrome_options.add_argument('--proxy-server=%s' % proxy_ip)
driver = webdriver.Remote(
command_executor="http://10.168.99.197:4444/wd/hub",
desired_capabilities=DesiredCapabilities.CHROME,
options=chrome_options
)
driver.get("http://www.baidu.com")
driver.maximize_window()
driver.save_screenshot('bd.png')
print(driver.title)
driver.close()
方式2:
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.proxy import Proxy, ProxyType
proxy = Proxy({
'proxyType': ProxyType.MANUAL,
'httpProxy': '10.168.162.95:8080',
'sslProxy': '10.168.162.95:8080',
'ftpProxy': '10.168.162.95:8080'
})
driver = webdriver.Remote(
command_executor="http://10.168.99.197:4444/wd/hub",
desired_capabilities=DesiredCapabilities.CHROME,
proxy=proxy
)
driver.get("http://www.baidu.com")
driver.maximize_window()
driver.save_screenshot('bd.png')
print(driver.title)
driver.close()
区别是方式二定制性更强,还可以设置用户名和密码。