SeleniumでプロキシIPを設定する方法を詳しく解説

高洛峰
リリース: 2017-03-26 16:43:51
オリジナル
7649 人が閲覧しました

Firefox でプロキシ IP を設定する

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')
ログイン後にコピー

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')
ログイン後にコピー

phantomjsプロキシ IP を設定する

方法 1: あまりにも洗練されていません (方法 2 を見てみましょう)

phantomjs では、上記の Firefox のようにすることはできませんでproxy のようなメソッド 2
phantomjs と Firefox はどちらも WebDriver から継承します。WebDriver が proxyphantomjs に渡すことができる親クラスは、WebDriver
の初期化時に proxy パラメーターを残さないため、以下に示すように phantomjs クラスのソース コードを変更できます。 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.
ログイン後にコピー

Seleniumでプロキシ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': 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()
ログイン後にコピー
方法 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': '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()
ログイン後にコピー
方法 3 (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()
ログイン後にコピー

以上がSeleniumでプロキシIPを設定する方法を詳しく解説の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!