在Python中使用代理运行Selenium Webdriver而不更改IP

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

我正在尝试使用 selenium 在 python 中创建代理,我有一个来自 https://free-proxy-list.net/ 的免费代理列表,但我得到的 IP 始终相同,这有问题设置?

PROXY = proxy_list[11]
options = Options()
options.headless = False

webdriver.DesiredCapabilities.FIREFOX['proxy'] = {
    "httpProxy":PROXY,
    "ftpProxy":PROXY,
    "sslProxy":PROXY,
    "noProxy":None,
    "proxyType":"MANUAL",
    "class":"org.openqa.selenium.Proxy",
    "autodetect":False
}
driver = webdriver.Firefox(options=options, executable_path=r'geckodriver.exe')
# Set the interceptor on the driver
driver.request_interceptor = interceptor 
driver.get(url)
time.sleep(5)
driver.quit()

编辑:我正在根据响应运行以下代码:

url = 'https://www.whatismyip.com/es/'
PROXY = proxy_list[6]
options = Options()
options.headless = False

firefox_capabilities = webdriver.DesiredCapabilities.FIREFOX
firefox_capabilities['marionette'] = True
firefox_capabilities['proxy'] = {
    'proxyType': "MANUAL",
    'httpProxy': PROXY,
    'ftpProxy': PROXY,
    'sslProxy': PROXY
}
driver = webdriver.Firefox(options=options, executable_path=r'geckodriver.exe', capabilities = firefox_capabilities)
# Set the interceptor on the driver
driver.request_interceptor = interceptor 
driver.get(url)
time.sleep(50)
driver.quit()

但是网页中的ip始终是相同的,不知道是否误解了代理的使用或者我做错了什么

python selenium proxy
2个回答
3
投票

如果您使用火狐浏览器,

...
# set proxy
firefox_capabilities = webdriver.DesiredCapabilities.FIREFOX
firefox_capabilities['marionette'] = True
firefox_capabilities['proxy'] = {
    'proxyType': "MANUAL",
    'httpProxy': PROXY,
    'ftpProxy': PROXY,
    'sslProxy': PROXY
}
driver = webdriver.Firefox(options=options, executable_path=r'geckodriver.exe', capabilities = firefox_capabilities)

如果您使用 Chrome,

...
chrome_options = Options()
chrome_options.add_argument('--proxy-server=' + PROXY)
chrome_options.add_argument("--headless") 
driver = webdriver.Chrome(executable_path = 'chromedriver.exe', options=chrome_options)

0
投票

https://stackoverflow.com/a/77555933/24675708 这对我在 Firefox 124.0.2 和 Selenium 4.19.0 上有效 (Selenium 4 中的语法已更改)

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