1. 程式人生 > >selenium設定代理(Phantomjs、Firefox、Chorme)

selenium設定代理(Phantomjs、Firefox、Chorme)

一、PhantomJs:

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

proxy = Proxy(
{
‘proxyType’: ProxyType.MANUAL,
‘httpProxy’: get_proxy_ip_port()
}
)
driver = webdriver.PhantomJS(
executable_path="/path/of/phantomjs",
proxy=proxy
)
driver.get(‘

http://httpbin.org/ip’)
print(driver.page_source)
driver.close()

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

proxy = Proxy(
{
‘proxyType’: ProxyType.MANUAL,
‘httpProxy’: ‘ip:port’ # 代理ip和埠
}
)
#新建一個‘期望技能’???
desired_capabilities = DesiredCapabilities.PHANTOMJS.copy()
#把代理ip加入到技能中
proxy.add_to_capabilities(desired_capabilities)
driver = webdriver.PhantomJS(
executable_path="/path/of/phantomjs",
desired_capabilities=desired_capabilities
)
driver.get(‘

http://httpbin.org/ip’)
print(driver.page_source)
driver.close()

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

proxy = Proxy(
{
‘proxyType’: ProxyType.MANUAL,
‘httpProxy’: ‘ip:port’ # 代理ip和埠
}
)
#新建一個“期望技能”,哈哈
desired_capabilities = DesiredCapabilities.PHANTOMJS.copy()
#把代理ip加入到技能中
proxy.add_to_capabilities(desired_capabilities)
driver = webdriver.PhantomJS(
executable_path="/path/of/phantomjs",
desired_capabilities=desired_capabilities
)
#測試一下
driver.get(‘

http://httpbin.org/ip’)
print driver.page_source

#現在開始切換ip
#再新建一個ip
proxy = Proxy(
{
‘proxyType’: ProxyType.MANUAL,
‘httpProxy’: ‘ip:port’ # 代理ip和埠
}
)
#再新建一個“期望技能”,()
desired_capabilities = DesiredCapabilities.PHANTOMJS.copy()
#把代理ip加入到技能中
proxy.add_to_capabilities(desired_capabilities)
#新建一個會話,並把技能傳入
driver.start_session(desired_capabilities)
driver.get(‘http://httpbin.org/ip’)
print driver.page_source
driver.close()

二、firefox
import time
from selenium.webdriver.common.proxy import*

myProxy = ‘202.202.90.20:8080’
proxy = Proxy({ ‘proxyType’: ProxyType.MANUAL, ‘httpProxy’: myProxy, ‘ftpProxy’: myProxy, ‘sslProxy’: myProxy, ‘noProxy’: ‘’ })

profile = webdriver.FirefoxProfile()
if proxy:
profile = get_firefox_profile_with_proxy_set(profile, proxy)
if user_agent:
profile.set_preference(“general.useragent.override”, user_agent)

driver=webdriver.Firefox(proxy=proxy,profile=profile)
driver.get(‘https://www.baidu.com’)
time.sleep(3)
driver.quit()

三、chorome

1.from selenium import webdriver
chromeOptions = webdriver.ChromeOptions()

#設定代理
chromeOptions.add_argument("–proxy-server=http://202.20.16.82:10152")

一定要注意,=兩邊不能有空格,不能是這樣–proxy-server = http://202.20.16.82:10152 browser =

webdriver.Chrome(chrome_options = chromeOptions)

檢視本機ip,檢視代理是否起作用

browser.get(“http://httpbin.org/ip”)
print(browser.page_source)

退出,清除瀏覽器快取

browser.quit()