1. 程式人生 > >selenium 代理設定

selenium 代理設定

設定Firefox代理:

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


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

driver = webdriver.Firefox(proxy = proxy, executable_path=r"/root/geckodriver")
driver.delete_all_cookies()
driver.maximize_window()

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

  

設定Chrome代理:

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


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('--proxy-server=http://%s' % proxy)
driver = webdriver.Chrome(executable_path=r"/root/chromedriver", chrome_options=ops)
driver.delete_all_cookies()
driver.maximize_window()

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