python-3.x - 在 WSL 上配置 Tor + Selenium 以进行网络抓取
问题描述
如何配置 Tor 以在 WSL 上与 Selenium 一起使用?
我正在尝试将Selenium + Geckodriver与Tor一起使用,但我似乎无法让它们一起玩得很好。
测试 1:使用 Selenium + geckodriver 驱动 Tor。
我正在研究python3
WSL。我已经将Tor和geckodriver下载到我的机器上。
from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
# Paths to Tor-Firefox and geckodriver executables (Windows):
firefox = '/mnt/c/Users/User/Downloads/Tor Browser/Browser/firefox.exe'
gecko = '/mnt/c/Program Files/Mozilla Firefox/geckodriver.exe'
# Create the webdriver.
binary = FirefoxBinary(firefox)
driver = webdriver.Firefox(firefox_binary=binary,executable_path=gecko) # Doesn't work.
# NOTE: a similar approach with Chrome + chromedriver + selenium works as expected.
未创建驱动程序,python 吐出以下错误消息:
SessionNotCreatedException:消息:找不到匹配的功能集
# but, its not a problem with the geckodriver.
# Show that geckodriver works...
driver = webdriver.Firefox(executable_path=gecko)
url="https://www.google.com/"
driver.get(url)
# But, it isn't working through tor.
driver.get('https://check.torproject.org') # Sorry, you are not using Tor.
测试 2:使用 torrequest 库随机化我的 IP 地址。
我可以用 随机化我的 IP 地址torrequest
,但它不被识别为 Tor。我已经安装 tor
并apt-get
完成了 torrc 文件的一些基本配置(1、2)。我设置了一个 HashedControlPasstor --hash-password <password>
并将其添加到我的torrc
. tor
我在命令行中启动了tor 。Tor 启动很吵,也许问题出在下面的警告上?
您的服务器 (IP:ADRESS:9001) 未能确认其 ORPort 可访问。在 ORPort 和 DirPort 可达之前,中继不会发布描述符。请检查您的防火墙、端口、地址、/etc/hosts 文件等。
尽管有警告,我可以用 torrequest 随机化我的 IP,但它不被识别为tor:
import requests
from torrequest import TorRequest
# Add HashedControlPass.
tr=TorRequest(password='<my HashedControlPass>')
# Check initial IP.
session = requests.session()
response = session.get('http://ipecho.net/plain')
ip = response.text
print("IP address is set to: {}".format(ip)) # my actual ip
# Check new IP with tor.
response = tr.get('http://ipecho.net/plain')
ip = response.text
print("IP address is set to: {}".format(ip)) # my ip is now different.
# Check if tor is active.
response = tr.get('https://check.torproject.org')
response.text # Sorry, you are not using Tor.
测试 3:在命令行上使用 tor (WSL-Ubuntu)。
尽管以前的方法有问题,但我可以torify
在命令行上成功使用。
torify wget 'https://check.torproject.org'
产生所需的响应:
恭喜。此浏览器配置为使用 Tor。
任何想法/帮助/建议表示赞赏。
解决方案
推荐阅读
- ffmpeg - 使用带有叠加网络摄像头的 ffmpeg 的 kmsgrab 设备记录 Linux Wayland/DRM 屏幕
- html - 不应该在移动菜单上显示的链接,在活动和悬停时
- angular - 如何在单个 index.html 文件中编译角度构建,该文件将从 http get 请求提供
- rust - 函数类型定义可以单独一行而不是内联吗?
- android - 将数据插入sqlite数据库时如何避免重复值?
- c# - 从 Azure 计算机视觉 API 提取文本时出错
- reactjs - 反应排除路线
- java - I want visible button has removed it text from TextView
- django - 来自芹菜任务延迟的Keyerror
- php - 使用 Google Optimize A/B 测试进行服务器端重定向