使用Selenium WebDriver+Tor作为代理

2024-09-27 19:29:22 发布

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

我试图使用SeleniumWebDriverFirefox通过9050端口的TorSocks5连接到一个特定的站点,但我无法建立连接。在

profile = FirefoxProfile()    
profile.set_preference('network.proxy.type', 1)
profile.set_preference( "network.proxy.socks_version", 5 )
profile.set_preference('network.proxy.socks', '127.0.0.1')
profile.set_preference('network.proxy.socks_port', 9050)
profile.set_preference( "network.proxy.socks_remote_dns", True )    
browser = webdriver.Firefox(firefox_profile=profile)

这个网站可能会阻止一些TOR连接,但奇怪的是我可以用TorBrowser连接到它!我甚至找到了TorBrowser使用的exit节点,并编辑了torrc文件以使用它(ExitNodes 'ip')。我检查了我的selenium Firefox的出口节点是否相同(我可以通过TOR代理成功连接到其他站点并检查我的ip),但我仍然无法连接,即使使用相同的ip!我的错误在哪里?在

第二件事是如果我设置了:

^{pr2}$

即使用TorBrowser代理,selenium Firefox成功地建立了与站点的连接。在

我的设置有问题吗?在


Tags: ip代理节点站点seleniumnetworkfirefoxprofile
1条回答
网友
1楼 · 发布于 2024-09-27 19:29:22

要使用Selenium WebDriver、GeckoDriver和Firefox Firefox通过端口9050上的TorSocks5连接到特定站点,可以使用FirefoxProfile来使用以下解决方案:

from selenium import webdriver
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
import os

torexe = os.popen(r'C:\Users\AtechM_03\Desktop\Tor Browser\Browser\TorBrowser\Tor\tor.exe')
profile = FirefoxProfile(r'C:\Users\AtechM_03\Desktop\Tor Browser\Browser\TorBrowser\Data\Browser\profile.default')
profile.set_preference('network.proxy.type', 1)
profile.set_preference('network.proxy.socks', '127.0.0.1')
profile.set_preference('network.proxy.socks_port', 9050)
profile.set_preference("network.proxy.socks_remote_dns", False)
profile.update_preferences()
driver = webdriver.Firefox(firefox_profile= profile, executable_path=r'C:\Utility\BrowserDrivers\geckodriver.exe')
driver.get("http://check.torproject.org")

相关问题 更多 >

    热门问题