设置proxy.socks.port selenium

2024-04-29 04:07:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我习惯于这样设置http端口:

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

这很管用。 但现在我需要连接socks代理并设置端口,它不起作用

profile.set_preference("network.proxy.socks_port", "PORTNUMBER")

我在文件里找不到推荐人,所以我在这里问。 有什么想法吗?有更好的办法吗?

谢谢


Tags: 文件端口http代理portnetworkprofileproxy
3条回答
ffprofile=webdriver.FirefoxProfile()
ffprofile.set_preference('network.proxy.type', 1)
ffprofile.set_preference('network.proxy.http', HTTP_IP)

ffprofile.set_preference("network.proxy.http_port", HTTPPORT)
ffprofile.set_preference('network.proxy.socks', 'SOCKS_IP')
ffprofile.set_preference('network.proxy.socks_port', SOCKSPORT)

ffprofile.update_preferences()

driver = webdriver.Remote(
   command_executor='http://SELENIUM:PORT/wd/hub',
   desired_capabilities=DesiredCapabilities.FIREFOX,
   browser_profile = ffprofile
   )

在您的情况下,我认为,应该将port用作int而不是string。详见下文

首先让我们了解FF(或与Selenium一起使用的webdriver)是如何设置SOCKS代理的。

对于Firefox,在URL框中执行about:config。

network.proxy.socks;10.10.10.1
network.proxy.socks_port;8999
network.proxy.socks_remote_dns;true
network.proxy.socks_version;5

您可以在FF profile director的prefs.js中看到相同的内容,如下所示:

user_pref("network.proxy.socks", "10.10.10.1");
user_pref("network.proxy.socks_port", 8999);
user_pref("network.proxy.type", 1);

请注意,network.proxy.socks是string,应该只设置为string。network.proxy.socks_port也必须是int

使用selenium python模块设置时:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.proxy import *
import time

# for fresh FF profile
#profile = webdriver.FirefoxProfile() 
profile_path="/path/to/custom/profile/"
profile = webdriver.FirefoxProfile(profile_path)
# set FF preference to socks proxy
profile.set_preference("network.proxy.type", 1)
profile.set_preference("network.proxy.socks", "10.10.10.1")
profile.set_preference("network.proxy.socks_port", 8999)
profile.set_preference("network.proxy.socks_version", 5)
profile.update_preferences()
driver = webdriver.Firefox(firefox_profile=profile)

driver.get("http://whatismyip.com")
print driver.page_source
# sleep if want to show in gui mode. we do print it in cmd
time.sleep(25)
driver.close()
driver.quit()

请检查是否支持给定的首选项,并在FF about:config列表中显示。

查看用户如何使用socks_端口。。。。在下面的例子中

using-selenium-for-web-based-hostname-enumerationgist.github.com

相关问题 更多 >