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()

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值