【问题标题】:Change proxy in Selenium Python在 Selenium Python 中更改代理
【发布时间】:2021-10-12 14:58:17
【问题描述】:

您好,我有一个用于旋转 firefox 代理的代码,我正在使用它,但它不会更改代理。

这是我的代码:

import time
import random
from selenium import webdriver
from selenium.webdriver.common.proxy import *
from http_request_randomizer.requests.proxy.requestProxy import RequestProxy

req_proxy = RequestProxy() #you may get different number of proxy when  you run this at each time
proxies = req_proxy.get_proxy_list() #this will create proxy list

change_proxy = proxies[0].get_address()
print("This is your ip and port: ", change_proxy)

ip = input("Ip: ")
port = input("Port: ")

profile = webdriver.FirefoxProfile()
profile.set_preference("network.proxy.type", 1)
profile.set_preference("network.proxy.http", ip)
profile.set_preference("network.proxy.http_port", port)
profile.update_preferences()

driver = webdriver.Firefox(firefox_profile=profile)
driver.get("https://whatismyipaddress.com/");

我将“change_proxy”的 ip 和端口部分输入到 ip 和端口,驱动程序打开 ip checker 网站,但我看到我的旧 ip 没有任何变化:

还有我原来的ip:

抱歉英语不好。

【问题讨论】:

  • 我在答案中添加了一些代理检查器代码,这可能对您有用。

标签: python selenium selenium-webdriver geckodriver selenium-firefoxdriver


【解决方案1】:

首先我要说的是,使用免费代理 IP 地址可能会有很大的问题。 这些类型的代理因存在连接问题而臭名昭著,例如与延迟相关的超时。此外,这些站点也可能是断断续续的,这意味着它们可以随时关闭。有时这些网站会被滥用,因此可能会被屏蔽。

我之前没有使用过 Python 包 http_request_randomizer,我注意到文档非常精简。我查看了包的源代码以了解其结构。

这里有一段代码使用http_request_randomizer获取随机HTTPS代理,传给geckodriver使用。

import random
import logging
from selenium import webdriver
from selenium.webdriver.common.proxy import Proxy
from selenium.webdriver.firefox.options import Options
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.firefox.options import FirefoxProfile
from selenium.webdriver.firefox.options import DesiredCapabilities
from http_request_randomizer.requests.proxy.ProxyObject import Protocol
from http_request_randomizer.requests.proxy.requestProxy import RequestProxy

# Obtain a list of HTTPS proxies
# Suppress the console debugging output by setting the log level
req_proxy = RequestProxy(log_level=logging.ERROR, protocol=Protocol.HTTPS)

# Obtain a random single proxy from the list of proxy addresses
random_proxy = random.sample(req_proxy.get_proxy_list(), 1)

firefox_options = Options()
firefox_options.add_argument("--disable-infobars")
firefox_options.add_argument("--disable-extensions")
firefox_options.add_argument("--disable-popup-blocking")

profile_options = FirefoxProfile()
user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 11.5; rv:90.0) Gecko/20100101 Firefox/90.0'
firefox_options.set_preference('profile_options = FirefoxProfile()', user_agent)

firefox_capabilities = DesiredCapabilities().FIREFOX

# add the random proxy to firefox_capabilities
firefox_proxies = Proxy()
firefox_proxies.ssl_proxy = random_proxy[0].get_address()
firefox_proxies.add_to_capabilities(firefox_capabilities)

driver = webdriver.Firefox(executable_path='/usr/local/bin/geckodriver', options=firefox_options,
                           desired_capabilities=firefox_capabilities)


# you can either do this
try:
    # print proxy IP for testing
    print(random_proxy[0].get_address())
    # output 
    46.151.145.4:53281

    URL = 'http://www.expressvpn.com/what-is-my-ip'
    driver.get(URL)

# Some of the proxies pulled from http_request_randomizer will timeout 
# for various reasons, so this exception is used to catch these timeouts
except TimeoutException as e:
    print("A Page load Timeout Occurred.")
    driver.quit()

# or this.  You can also put this in a try/except block and 
# increase the timeout as needed.
# 
# driver.set_page_load_timeout(120)
# URL = 'http://www.expressvpn.com/what-is-my-ip'
# driver.get(URL)

这是一个屏幕截图,显示 Firefox 正确使用了此会话的 IP 地址。

如前所述,免费代理可能有多个问题。下面的代码展示了如何使用proxy judge 来检查单个代理的状态。

import random
import logging
from time import sleep
from random import randint
from proxy_checking import ProxyChecker
from http_request_randomizer.requests.proxy.ProxyObject import Protocol
from http_request_randomizer.requests.proxy.requestProxy import RequestProxy


def random_ssl_proxy_address():
    # Obtain a list of HTTPS proxies
    # Suppress the console debugging output by setting the log level
    req_proxy = RequestProxy(log_level=logging.ERROR, protocol=Protocol.HTTPS)

    # Obtain a random single proxy from the list of proxy addresses
    random_proxy = random.sample(req_proxy.get_proxy_list(), 1)

    return random_proxy[0].get_address()


def get_proxy_address():
    proxy_address = random_ssl_proxy_address()
    checker = ProxyChecker()
    proxy_judge = checker.check_proxy(proxy_address)
    proxy_status = [value for key, value in proxy_judge.items() if key == 'status']

    if proxy_status[0]:
        return proxy_address
    else:
        print('Looking for a valid proxy address.')

        # this sleep timer is helping with some timeout issues
        # that were happening when querying
        sleep(randint(5, 10))

        get_proxy_address()


random_ssl_proxy = get_proxy_address()
print(f'Valid proxy address: {random_ssl_proxy}')
# output
Valid proxy address: 98.116.152.143:3128

请注意,我使用的 proxy_checker 包没有任何嵌入式错误处理,因此您必须添加一些来捕获一些错误。

【讨论】:

  • 您好!我测试了你的代码大约 20 次,但页面没有加载 :( 我找到了另一个代码,但它是用于“chrome”的,它可以工作,但我需要在 geckodriver 上这样做,因为我的 chromedriver 太慢了。我把错误问了。
  • 就像我在回答中所说的那样,一些免费代理将无法正常工作。我刚刚再次运行代码并得到了这个代理 `96.9.69.164:53281`。当我交叉引用此代理时,它处于离线状态,并且已经有一段时间了。
  • 当我运行另一个测试时,这个地址被选为119.81.189.194:80,这也不起作用。必须添加额外的代码来验证每个代理的状态。
  • 嗨,我再次测试了代码 100 次,但它没有再次启动链接。我为 chromedriver 尝试了另一个代码,它在第一次启动时工作。我认为代理没有问题。请帮助我如何修复thissssssss。谢谢...
  • 代理是问题所在,因为其中一些会超时。您可以添加驱动程序超时,这可能会有所帮助。我将更新我的答案以使用一个。
猜你喜欢
  • 2014-02-24
  • 1970-01-01
  • 2018-08-28
  • 2015-06-28
  • 1970-01-01
  • 2023-01-30
  • 1970-01-01
  • 2020-10-10
  • 2017-11-24
相关资源
最近更新 更多