繁体   English   中英

Python Selenium 与 TOR 浏览器的绑定

[英]Python Selenium binding with TOR browser

我研究了它,但我得到了解决方案:

from selenium import webdriver
profile = webdriver.FirefoxProfile()
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)
driver = webdriver.Firefox(profile)
driver.get('http://estoeslapollaconcebol.la')

它给出了这个错误:

无法加载配置文件。 配置文件目录:C:\\Users\\HPPAV1~1\\AppData\\Local\\Temp\\tmppcuwx3xd Firefox 输出:无

当我尝试该解决方案时。

from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
profile=webdriver.FirefoxProfile('C:\\Users\\HP PAV 15\\Desktop\\Tor     Browser\\Browser\\TorBrowser\\Data\\Browser\\profile.default\\')
binary =FirefoxBinary('C:\\Users\\HP PAV 15\\Desktop\\Tor Browser\\Browser\\firefox')
#browser = binary.launch_browser(profile)
profile.set_preference('network.proxy.type', 1)
profile.set_preference('network.proxy.socks', '127.0.0.1')
profile.set_preference('network.proxy.socks_port', 9150)
browser=webdriver.Firefox( binary, profile)
browser.get("http://yahoo.com")
browser.save_screenshot("/Users/admin/Pictures/screenshot.png")
browser.close()

它给了我以下错误:

回溯(最近一次调用):文件“C:/Python34/torfirstscript.py”,第 10 行,在 browser=webdriver.Firefox( binary, profile) 文件“C:\\Python34\\lib\\site-packages\\selenium-2.43 .0-py3.4.egg\\selenium\\webdriver\\firefox\\webdriver.py",第 46 行,在init self.NATIVE_EVENTS_ALLOWED 和 self.profile.native_events_enabled) AttributeError: 'FirefoxBinary' 对象没有属性 'native_events_enabled'

通过应用

browser=webdriver.Firefox( firefox_binary = binary, firefox_profile = profile)

我收到此错误:

回溯(最近一次调用):文件“C:\\Python34\\torfirstscript.py”,第 9 行,在 browser=webdriver.Firefox( firefox_binary = binary, firefox_profile = >profile) 文件“C:\\Python34\\lib\\site-包\\selenium-2.43.0->py3.4.egg\\selenium\\webdriver\\firefox\\webdriver.py”,第 59 行,在init self.binary 中,超时),文件“C:\\Python34\\lib\\site-packages \\selenium-2.43.0->py3.4.egg\\selenium\\webdriver\\firefox\\extension_connection.py”,第 47 行,在 > init self.binary.launch_browser(self.profile) 文件“C:\\Python34\\lib\\ site-packages\\selenium-2.43.0->py3.4.egg\\selenium\\webdriver\\firefox\\firefox_binary.py”,第 64 行,在 launch_browser self._wait_until_connectable() 文件“C:\\Python34\\lib\\site-packages \\selenium-2.43.0-py3.4.egg\\selenium\\webdriver\\firefox\\firefox_binary.py",第 108 行,在 _wait_until_connectable self.profile.path, self._get_firefox_output())) selenium.common.exceptions.WebDriverException:消息:“无法加载配置文件。配置文件目录:>C:\\Users\\HPPAV1~1\\AppData\\Local\\Temp\\tmpig7zvx_0\\webdriver -py-profilecopy Firefox 输出:无”

以该图像作为输出。

在此处输入图片说明

在 Windows 上使用 Selenium 和 Tor 的工作示例:

from selenium import webdriver
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

binary = FirefoxBinary(r"C:\Program Files (x86)\TorBrowser\Browser\firefox.exe")
profile = FirefoxProfile(r"C:\Program Files (x86)\TorBrowser\Browser\TorBrowser\Data\Browser\profile.default")

driver = webdriver.Firefox(profile, binary)
driver.get("http://stackoverflow.com")
driver.save_screenshot("screenshot.png")
driver.quit()

使用以下方法更新硒:

pip install -U selenium

然后运行你的代码,当然是在启动 TOR 之后。 已确认并修复此错误。

PS:如果您使用的是 Linux,请不要忘记“Sudo”。

Windows 上最新 TOR 安装的代码:

from selenium import webdriver
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

binary = FirefoxBinary(r"C:\Users\<Windows User>\Desktop\Tor Browser\Browser\firefox.exe")
profile = FirefoxProfile(r"C:\Users\<Windows User>\Desktop\Tor Browser\Browser\TorBrowser\Data\Browser\profile.default")

driver = webdriver.Firefox(profile, binary)
driver.get("http://stackoverflow.com")

我试过这样的事情,并工作:

profile = webdriver.FirefoxProfile()
profile.set_preference('network.proxy.type', 1)
profile.set_preference('network.proxy.socks', '127.0.0.1')
profile.set_preference('network.proxy.socks_port', 9150)
driver = webdriver.Firefox(profile)

执行此操作时打开 Tor 浏览器

另一个简单的解决方案是:在 Firefox 或 Chrome 中创建一个新的配置文件,将浏览器配置为使用 Tor 代理(设置 SOCKS 5 代理以解决 127.0.0.1 端口 9150),然后在使用 webdriver 时加载该配置文件。

您正在将FirefoxBinary实例作为Firefox构造函数的第一个位置参数传递,但根据定义Firefox期望FirefoxProfile实例作为第一个位置参数。

相反,只需使用关键字参数:

browser = webdriver.Firefox(firefox_binary=binary, firefox_profile=profile)

这给可读性带来了很小的好处。

我在 Windows 上解决了类似的问题:

from selenium import webdriver
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

binary = FirefoxBinary(r"C:\Users\<Windows User>\Desktop\Tor Browser\Browser\firefox.exe")

driver = webdriver.Firefox(firefox_binary=binary)
driver.profile.set_preference('network.proxy.type', 1)
driver.profile.set_preference('network.proxy.socks', '127.0.0.1')
driver.profile.set_preference('network.proxy.socks_port', 9051)

driver.get("http://stackoverflow.com")

由于其中一些方法在当前的 Windows 版本中不起作用,返回“tor failed to start”错误会通知用户,为了启动代理,他们需要在执行脚本之前已经运行了 tor。

这从 2020 年 5 月 12 日起生效。 在运行此脚本之前,您需要运行 Tor 浏览器。 这将在 Chrome 中运行 Tor。 只会在隐身模式下执行此操作。 如果您删除该选项,它将通过您的 isp 连接。

import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options


tor_proxy = "127.0.0.1:9150"

chrome_options = Options()

'''chrome_options.add_argument("--test-type")'''
chrome_options.add_argument('--ignore-certificate-errors')
'''chrome_options.add_argument('--disable-extensions')'''
chrome_options.add_argument('disable-infobars')
'''chrome_options.add_argument("--incognito")'''
chrome_options.add_argument('--user-data=C:\\Users\\user\\AppData\\Local\\Google\\Chrome\\User Data\\Default')
chrome_options.add_argument('--proxy-server=socks5://%s' % tor_proxy)
driver = webdriver.Chrome(executable_path='C:\\chromedriver.exe', options=chrome_options)
driver.get('https://www.google.com')
time.sleep(4)
driver.switch_to.frame(0)
driver.find_element_by_id("introAgreeButton").click()
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium import webdriver

caps = DesiredCapabilities.FIREFOX
caps['proxy'] = {
    'proxyType': 'MANUAL',
    'socksProxy': '127.0.0.1:9050',
    'socksVersion': 5
}

driver = webdriver.Firefox(executable_path=r"C:\webdrivers\geckodriver.exe", capabilities=caps)

就我而言,此代码是唯一有效的代码。

这对我有用,它不使用 Tor 浏览器,而是使用 geckodriver

pip install selenium webdriver-manager

import asyncio
import os
import subprocess

from selenium.webdriver import Firefox
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.firefox.service import Service
from webdriver_manager.firefox import GeckoDriverManager

profile_path = os.path.expandvars(
    r"%USERPROFILE%\Desktop\Tor Browser\Browser\TorBrowser\Data\Browser\profile.default"
)
options = Options()
options.set_preference("profile", profile_path)
service = Service(
    # os.path.expandvars(r"%USERPROFILE%\Desktop\Tor Browser\Browser\firefox.exe"),
    executable_path=GeckoDriverManager().install()
)

options.set_preference("network.proxy.type", 1)
options.set_preference("network.proxy.socks", "127.0.0.1")
options.set_preference("network.proxy.socks_port", 9050)
options.set_preference("network.proxy.socks_remote_dns", False)


async def main():
    async def cleanup():
        driver.quit()
        print(torexe.pid)
        torexe.kill()

    try:
        # https://stackoverflow.com/a/62686067/8608146
        torexe = subprocess.Popen(
            os.path.expandvars(
                r"%USERPROFILE%\Desktop\Tor Browser\Browser\TorBrowser\Tor\tor.exe"
            )
        )
        driver = Firefox(service=service, options=options)
        driver.get("https://check.torproject.org")
        driver.save_screenshot("screenshot.png")
    except Exception as e:
        print(e, type(e))
    finally:
        await cleanup()


if __name__ == "__main__":
    asyncio.run(main())

暂无
暂无

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

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