Firefox GeckoDriver不接受带有用户身份验证的HTTP代理IP

问题描述 投票:0回答:1

所以我有下面的代码,它仍然显示真实的IP。也不产生错误。抱歉,无法分享真实的代理详细信息:)

from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.proxy import Proxy, ProxyType
from selenium.webdriver.firefox.options import Options
PROXY_HOST = "206.41.127.230"

PROXY_PORT = "603230"

USERNAME = "xxx"

PASSWORD = "xx"

profile = webdriver.FirefoxProfile()

profile.set_preference("network.proxy.type", 1)

profile.set_preference("network.proxy.http", PROXY_HOST)

profile.set_preference("network.proxy.http_port", PROXY_PORT)

profile.set_preference("network.proxy.socks_username", USERNAME)

profile.set_preference("network.proxy.socks_password", PASSWORD)

options = Options()
options.headless = True
driver = webdriver.Firefox(options=options, executable_path=r'/usr/local/bin/geckodriver', firefox_profile=profile)
driver.get('https://httpbin.org/ip')
html = driver.page_source
print(html)
driver.quit()
python selenium http-proxy geckodriver firefox-profile
1个回答
0
投票

似乎您很近。您需要为FirefoxProfile实例update_preferences()调用profile,然后可以使用以下解决方案:

from selenium import webdriver

PROXY_HOST = "206.41.127.230"
PROXY_PORT = "6032"
USERNAME = "xxx"
PASSWORD = "xx"

profile = webdriver.FirefoxProfile()
profile.set_preference('network.proxy.type', 1)
profile.set_preference("network.proxy.http", PROXY_HOST)
profile.set_preference("network.proxy.http_port", PROXY_PORT)
profile.set_preference("network.proxy.socks_username", USERNAME)
profile.set_preference("network.proxy.socks_password", PASSWORD)
profile.update_preferences()
options = webdriver.FirefoxOptions()
options.headless = True
driver = webdriver.Firefox(options=options, executable_path=r'/usr/local/bin/geckodriver', firefox_profile=profile)
driver.get('https://httpbin.org/ip')
html = driver.page_source
print(html)
driver.quit()

参考

您可以在以下位置找到相关的讨论:

© www.soinside.com 2019 - 2024. All rights reserved.