如何通过 Selenium 在 Chrome 浏览器中使用 Tor [英] How to use Tor with Chrome browser through Selenium
问题描述
我正在尝试在 Tor 上运行我的 selenium 驱动程序.请注意,该脚本在没有 Tor 的情况下已经运行且没有错误.
这是我目前所做的:
1) 我调用了 Tor 框架
进口袜子进口插座从 stem.util 导入术语进口茎.过程SOCKS_PORT=7000socks.setdefaultproxy(proxy_type=socks.PROXY_TYPE_SOCKS5,地址 = "127.0.0.1",端口 = SOCKS_PORT)socket.socket = 袜子.socksocket# 通过socket进行DNS解析def getaddrinfo(*args): return [(socket.AF_INET, socket.SOCK_STREAM, 6, '', (args[0], args[1]))]socket.getaddrinfo = 获取地址信息def print_bootstrap_lines(line):如果自举"在行:打印(term.format(line,term.Color.GREEN))tor_process = stem.process.launch_tor_with_config(tor_cmd = "C:/Users/my-usernameDesktop/Tor Browser/Browser/TorBrowser/Tor//tor.exe" ,config = { 'SocksPort': str(SOCKS_PORT),},init_msg_handler = print_bootstrap_lines,)
在调用 Tor 框架(据我所知就像一个容器)之后,我调用了 Chrome 驱动程序:
from selenium import webdriver从 selenium.webdriver.common.keys 导入密钥从 selenium.webdriver.chrome.options 导入选项选项 = 选项()options.binary_location = r'C:Program Files (x86)GoogleChromeApplicationchrome.exe'driver = webdriver.Chrome(options=options, executable_path = r'C:Usersmy-usernamechromedriver')
3) 此时我插入抓取脚本.
4) 关闭驱动并杀死 Tor 进程:
driver.close()tor_process.kill()
我得到的输出如下:
Apr 15 14:31:20.000 [notice] 自举 0%:开始4 月 15 日 14:31:23.000 [通知] 引导 10%:完成与目录服务器的握手4 月 15 日 14:31:23.000 [通知] 自举 80%:连接到 Tor 网络4 月 15 日 14:31:23.000 [通知] 自举 90%:建立 Tor 电路4 月 15 日 14:31:24.000 [通知] 引导 100%:完成回溯(最近一次调用最后一次):文件<ipython-input-2-2b2233fc0ae4>",第 1 行,在 <module> 中runfile('C:/Users/my-username-folder/FireStarter_All_1Step_2.py', wdir='C:/Users/my-username-folder')运行文件中的文件C:Usersmy-usernameAppDataLocalContinuumanaconda3libsite-packagesspyder_kernelscustomizespydercustomize.py",第 786 行execfile(文件名,命名空间)文件C:Usersmy-usernameAppDataLocalContinuumanaconda3libsite-packagesspyder_kernelscustomizespydercustomize.py",第 110 行,在 execfile 中exec(编译(f.read(),文件名,'exec'),命名空间)文件C:/Users/my-username-folder/FireStarter_All_1Step_2.py",第 94 行,在 <module>driver = webdriver.Chrome(options=options, executable_path = r'C:Usersmy-username-folderchromedriver')文件C:Usersmy-usernameAppDataLocalContinuumanaconda3libsite-packagesseleniumwebdriverchromewebdriver.py",第 73 行,在 __init__self.service.start()文件C:Usersmy-usernameAppDataLocalContinuumanaconda3libsite-packagesseleniumwebdrivercommonservice.py",第 104 行,在开始raise WebDriverException("无法连接到服务 %s" % self.path)WebDriverException:无法连接到服务 C:Usersmy-username-folderchromedriver
我做错了什么?
更新:我希望在 Chrome 浏览器中使用 Tor.
通过
<小时><块引用>您可以在 How to 中找到相关讨论使用 Python 连接 Tor 浏览器
I'm trying to run my selenium driver on Tor. Note that the script already runs with no errors without Tor.
This is what I've done so far:
1) I called the Tor framework
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-usernameDesktop/Tor Browser/Browser/TorBrowser/Tor//tor.exe" ,
config = { 'SocksPort': str(SOCKS_PORT),},
init_msg_handler = print_bootstrap_lines,
)
After calling the Tor framework which is like a container to my understanding, I then called the Chrome driver:
from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver.chrome.options import Options options = Options() options.binary_location = r'C:Program Files (x86)GoogleChromeApplicationchrome.exe' driver = webdriver.Chrome(options=options, executable_path = r'C:Usersmy-usernamechromedriver')
3) At this point I insert the scraping script.
4) Close driver and kill the Tor process:
driver.close()
tor_process.kill()
The output I get is the following:
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:Usersmy-usernameAppDataLocalContinuumanaconda3libsite-packagesspyder_kernelscustomizespydercustomize.py", line 786, in runfile
execfile(filename, namespace)
File "C:Usersmy-usernameAppDataLocalContinuumanaconda3libsite-packagesspyder_kernelscustomizespydercustomize.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:Usersmy-username-folderchromedriver')
File "C:Usersmy-usernameAppDataLocalContinuumanaconda3libsite-packagesseleniumwebdriverchromewebdriver.py", line 73, in __init__
self.service.start()
File "C:Usersmy-usernameAppDataLocalContinuumanaconda3libsite-packagesseleniumwebdrivercommonservice.py", line 104, in start
raise WebDriverException("Can not connect to the Service %s" % self.path)
WebDriverException: Can not connect to the Service C:Usersmy-username-folderchromedriver
What am I doing wrong?
Update: I am looking to use Tor with Chrome browser.
To use Tor with Chrome browser through Selenium you can use the following solution:
Code Block:
from selenium import webdriver import os # To use Tor's SOCKS proxy server with chrome, include the socks protocol in the scheme with the --proxy-server option # PROXY = "socks5://127.0.0.1:9150" # IP:PORT or HOST:PORT torexe = os.popen(r'C:UsersDebanjan.BDesktopTor BrowserBrowserTorBrowserTor or.exe') PROXY = "socks5://localhost:9050" # IP:PORT or HOST:PORT options = webdriver.ChromeOptions() options.add_argument('--proxy-server=%s' % PROXY) driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:UtilityBrowserDriverschromedriver.exe') driver.get("http://check.torproject.org")
Browser Snapshot:
You can find a relevant discussion in How to connect to Tor browser using Python
这篇关于如何通过 Selenium 在 Chrome 浏览器中使用 Tor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!