![](/img/trans.png)
[英]How to set SOCKS5 proxy with auth for Chrome in selenium Python?
[英]How can I set socks5 proxy for selenium webdriver? Python
我真的無法在 selenium for python 中為我的 chrome webdriver 設置 socks5 代理(http too ...)。 我嘗試了很多不同的方法......但我認為我做錯了。
示例 1:
self.options.add_argument('--proxy-server=http://'+proxy)
示例 2:
webdriver.DesiredCapabilities.CHROME['proxy'] = {
"socksProxy": proxy,
"ftpProxy": proxy,
"sslProxy": proxy,
"noProxy": None,
"proxyType": "MANUAL",
"class": "org.openqa.selenium.Proxy",
"autodetect": False
}
請完整描述在 Selenium 上為 Python 和 Chrome webdriver 設置 socks5 代理的工作示例,並附上代理字符串格式的示例(也許我在這里做錯了什么……)。
PS我得到的兩個問題:
Chrome 不允許使用身份驗證的代理。 我不舒爾,但在閱讀了這么多信息后,我認為是這樣......只有一種方法對我有用 - 通過登錄名和密碼使用沒有身份驗證的代理 socks5。
options = webdriver.ChromeOptions()
proxy = '12.12.421.125:1949'
options.add_argument('--proxy-server=socks5://' + proxy)
driver = webdriver.Chrome(options=options)
對於FireFox 的 geckodriver ,如果您只想設置socks5 主機/socks5 代理:-
form selenium import webdriver
profile = webdriver.FirefoxProfile()
# Socks5 Host SetUp:-
myProxy = "198.199.101.152:8388"
ip, port = myProxy.split(':')
profile.set_preference('network.proxy.type', 1)
profile.set_preference('network.proxy.socks', ip)
profile.set_preference('network.proxy.socks_port', int(port))
driver = webdriver.Firefox(firefox_profile=profile)
這是我用來使用用戶名/密碼身份驗證連接到 Socks5 服務器的代碼。
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
capabilities = dict(DesiredCapabilities.CHROME)
capabilities['proxy'] = {
'proxyType': 'MANUAL',
'socksProxy': '<Your_IP>:<Your_Port>',
'socksVersion': 5,
'ftpProxy': '<Your_IP>:<Your_Port>',
'noProxy': 'localhost,127.0.0.1',
'class': "org.openqa.selenium.Proxy",
'autodetect': False
}
capabilities['proxy']['socksUsername'] = '<username>'
capabilities['proxy']['socksPassword'] = '<password>'
driver = Chrome(ChromeDriverManager().install(), desired_capabilities=capabilities)
'proxy': {
'http': 'socks5://user:pass@192.168.10.100:8888',
'https': 'socks5://user:pass@192.168.10.100:8888',
'no_proxy': 'localhost,127.0.0.1'
}
}
driver = webdriver.Chrome(seleniumwire_options=options)```
source - https://github.com/wkeeling/selenium-wire#socks
對於Firefox 的 geckodriver ,根據Tanmay Harsh 的回答,到目前為止我已經得到了這個:
from selenium.webdriver import Firefox, FirefoxOptions
proxy = ('proxy-server.local', 1080)
options = FirefoxOptions()
options.set_preference('network.proxy.type', 1)
options.set_preference('network.proxy.socks', proxy[0])
options.set_preference('network.proxy.socks_port', proxy[1])
options.set_preference('network.proxy.socks_remote_dns', True)
driver = Firefox(options=options)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.