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



我正在尝试使用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总是一样的,不知道是误解了代理的使用还是我做错了什么

如果您使用Firefox浏览器,

...
# 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)

最新更新