Detailed explanation of how to set proxy IP in selenium

高洛峰
Release: 2017-03-26 16:43:51
Original
7709 people have browsed it

Set proxy ip in Firefox

method_1

from selenium import webdriver
profile = webdriver.FirefoxProfile()
profile.set_preference('network.proxy.type', 1)
profile.set_preference('network.proxy.http', '127.0.0.1')
profile.set_preference('network.proxy.http_port', 17890)  # int
profile.update_preferences()
driver = webdriver.Firefox(firefox_profile=profile)
driver.get('http://httpbin.org/ip')
Copy after login

method_2

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.Firefox(proxy=proxy)
driver.get('http://httpbin.org/ip')
Copy after login

phantomjsSet proxy ip

Method 1 : Too inelegant (Let’s look at method 2)

In phantomjs, you cannot pass in proxy like Firefox’s method2 above
Both phantomjs and FirefoxInheritfrom WebDriver , the parent class WebDriver can pass in proxy
phantomjs does not leave the proxy parameter when initializing WebDriver
So you can change the source code of the phantomjs class as shown below, and you can pass in the proxy parameter in phantomjs

# 注意授权
# Licensed to the Software Freedom Conservancy (SFC) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The SFC licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
Copy after login

Detailed explanation of how to set proxy IP in selenium

The following is an example
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()
Copy after login
Method two:
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()
Copy after login
Method three (dynamic switching ip):
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()
Copy after login

The above is the detailed content of Detailed explanation of how to set proxy IP in selenium. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!