设置Firefox代理

# -*- coding: utf-8 -*-

from selenium import webdriver
from selenium.webdriver.common.proxy import Proxy, ProxyType

my_proxy = self.get_proxy().strip()

proxy = Proxy({
'proxyType': ProxyType.MANUAL,
'httpProxy': my_proxy,
'noProxy': ''
})

driver = webdriver.Firefox(proxy = proxy, executable_path="C:/spider/geckodriver.exe")
driver.delete_all_cookies()
driver.maximize_window()

driver.get("http://httpbin.org/ip")
print(driver.page_source)
driver.quit()

设置Chrome代理

# -*- coding: utf-8 -*-

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

proxy = self.get_proxy().strip()
ops = Options()
# ops.add_argument('--headless')
# ops.add_argument('--no-sandbox')
# ops.add_argument('--disable-dev-shm-usage')
# ops.add_argument('--disable-gpu')
print('--proxy-server=http://%s' % proxy)
ops.add_argument('--user-agent=%s' % ua)
# ops.add_argument('--user-data-dir=C:/Users/Administrator/AppData/Local/Google/Chrome/User Data')
ops.add_argument('--proxy-server=http://%s' % proxy)
driver = webdriver.Chrome(executable_path="C:/spider/chromedriver.exe", chrome_options=ops)
driver.delete_all_cookies()
driver.maximize_window()

driver.get("http://httpbin.org/ip")
print(driver.page_source)
driver.quit()