问题:如何通过 Selenium 在 Chrome 浏览器中使用 Tor

我正在尝试在 Tor 上运行我的 selenium 驱动程序。请注意,在没有 Tor 的情况下,该脚本已经运行且没有错误。

这是我到目前为止所做的:

1)我调用了 Tor 框架

import socks
import socket
from stem.util import term    


import stem.process

SOCKS_PORT=7000 

socks.setdefaultproxy(proxy_type=socks.PROXY_TYPE_SOCKS5,
                      addr = "127.0.0.1", 
                      port = SOCKS_PORT)
socket.socket = socks.socksocket

# Perform DNS resolution through the socket
def getaddrinfo(*args):   return [(socket.AF_INET, socket.SOCK_STREAM, 6, '', (args[0], args[1]))]

socket.getaddrinfo = getaddrinfo

def print_bootstrap_lines(line):   
    if "Bootstrapped " in line:
      print(term.format(line, term.Color.GREEN))

tor_process = stem.process.launch_tor_with_config(
    tor_cmd = "C:/Users/my-username\Desktop/Tor Browser/Browser/TorBrowser/Tor//tor.exe" ,
    config = { 'SocksPort': str(SOCKS_PORT),},
    init_msg_handler = print_bootstrap_lines,
)

2.调用Tor框架后,我理解为一个容器,然后调用Chrome驱动:

从硒导入网络驱动程序
从 selenium.webdriver.common.keys 导入密钥
从 selenium.webdriver.chrome.options 导入选项
选项u003d选项()
options.binary_location u003d r'C:\Program Files (x86)\Google\Chrome\Application
driver u003d webdriver.Chrome(optionsu003doptions, executable_path u003d r'C:\Users\my-usernam

3)此时我插入了抓取脚本。

  1. 关闭驱动程序并终止 Tor 进程:
driver.close()   
tor_process.kill()

我得到的输出如下:

Apr 15 14:31:20.000 [notice] Bootstrapped 0%: Starting
Apr 15 14:31:23.000 [notice] Bootstrapped 10%: Finishing handshake with directory server
Apr 15 14:31:23.000 [notice] Bootstrapped 80%: Connecting to the Tor network
Apr 15 14:31:23.000 [notice] Bootstrapped 90%: Establishing a Tor circuit
Apr 15 14:31:24.000 [notice] Bootstrapped 100%: Done
Traceback (most recent call last):

  File "<ipython-input-2-2b2233fc0ae4>", line 1, in <module>
    runfile('C:/Users/my-username-folder/FireStarter_All_1Step_2.py', wdir='C:/Users/my-username-folder')

  File "C:\Users\my-username\AppData\Local\Continuum\anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 786, in runfile
    execfile(filename, namespace)

  File "C:\Users\my-username\AppData\Local\Continuum\anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/my-username-folder/FireStarter_All_1Step_2.py", line 94, in <module>
    driver = webdriver.Chrome(options=options, executable_path = r'C:\Users\my-username-folder\chromedriver')

  File "C:\Users\my-username\AppData\Local\Continuum\anaconda3\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 73, in __init__
    self.service.start()

  File "C:\Users\my-username\AppData\Local\Continuum\anaconda3\lib\site-packages\selenium\webdriver\common\service.py", line 104, in start
    raise WebDriverException("Can not connect to the Service %s" % self.path)

WebDriverException: Can not connect to the Service C:\Users\my-username-folder\chromedriver

我究竟做错了什么?

更新:我希望将 Tor 与 Chrome 浏览器一起使用。

解答

要通过Selenium将 TorChrome 浏览器一起使用,您可以使用以下解决方案:

  • 代码块:
从硒导入网络驱动程序
进口我们

# 要在 chrome 中使用 Tor 的 SOCKS 代理服务器,请在方案中包含带有 --proxy-server 选项的 socks 协议
# PROXY u003d "socks5://127.0.0.1:9150" # IP:PORT 或 HOST:PORT

torexe u003d os.popen(r'C:\Users\Debanjan.B\Desktop\Tor Browser\Browser\TorBrowser\Tor or.exe')
PROXY u003d "socks5://localhost:9050" # IP:PORT 或 HOST:PORT
选项 u003d webdriver.ChromeOptions()
options.add_argument('--proxy-serveru003d%s' % PROXY)
driver u003d webdriver.Chrome(chrome_optionsu003doptions, executable_pathu003dr'C:\Utility\BrowserDriver
driver.get("http://check.torproject.org")
  • 浏览器快照:

Tor_Chrome


可以在如何使用Python连接Tor浏览器中找到相关讨论

Logo

Python社区为您提供最前沿的新闻资讯和知识内容

更多推荐