셀레늄에서 프록시 IP를 설정하는 방법에 대한 자세한 설명

高洛峰
풀어 주다: 2017-03-26 16:43:51
원래의
7676명이 탐색했습니다.

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의 방법 2와 같은 프록시를 전달할 수 없습니다
phantomjs와 Firefox 모두 WebDriver에서 상속합니다. 상위 클래스 WebDriver는 프록시를 전달할 수 있습니다
phantomjs는 WebDriver 초기화 시 프록시 매개변수를 남기지 않습니다
. 따라서 아래와 같이 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.
로그인 후 복사

셀레늄에서 프록시 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 전환):
아아아아

위 내용은 셀레늄에서 프록시 IP를 설정하는 방법에 대한 자세한 설명의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!