[英]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.