简体   繁体   English

使用Selenium WebDriver + Tor作为代理

[英]Using Selenium WebDriver + Tor as proxy

I try to connect to a specific site using Selenium WebDriver Firefox through TOR Socks5 at 9050 port and I can't establish the connection. 我尝试使用Selenium WebDriver Firefox通过TOR Socks5在9050端口连接到特定站点,但无法建立连接。

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)

The site is probably blocking some TOR connections but the odd thing is that I can connect to it using TorBrowser! 该站点可能阻止了一些TOR连接,但奇怪的是我可以使用TorBrowser连接到它! I even found the exit node that was used by TorBrowser and edited my torrc file to use it too ( ExitNodes 'ip' ). 我什至找到了TorBrowser使用的出口节点,并编辑了torrc文件以使用它( ExitNodes 'ip' )。 I checked that my selenium Firefox's exit node was the same (I can successfuly connect to other sites through TOR proxy and check my ip), but I still can't connect, even using the same ip! 我检查了我的Firefox硒出口节点是否相同(我可以通过TOR代理成功连接到其他站点并检查我的IP),但是即使使用相同的IP,我仍然无法连接! Where is my mistake? 我的错误在哪里?

And the second thing is that if I set up: 第二件事是,如果我设置了:

profile.set_preference('network.proxy.socks_port', 9150) 

ie use TorBrowser proxy, selenium Firefox successfully establishes connection to the site . 即使用TorBrowser代理,Firefox硒将成功建立与站点的连接。

Is it something wrong with my tor settings? 我的Tor设定有问题吗?

To connect to a specific site using Selenium WebDriver , GeckoDriver and Firefox through TOR Socks5 on port 9050 you can start the tor daemon and using a FirefoxProfile you can use the following solution: 要使用Selenium WebDriverGeckoDriverFirefox通过端口9050上的TOR Socks5连接到特定站点,您可以启动tor守护程序,并使用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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM