How to initiate Tor Browser using Selenium and Pyt

2019-08-16 20:42发布

I m trying to open the webpage in Tor Browser using Python

Code:

# Start :Code For TOR Browser
from selenium import webdriver
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

# path to TOR binary
binary = FirefoxBinary(r'C:\\Tor Browser\\Browser\\firefox.exe')
# binary = FirefoxBinary(r'C:\\Program Files (x86)\\Vidalia Bridge Bundle\\Tor\\tor.exe')
#path to TOR profile
profile = FirefoxProfile(r'C:\\Tor Browser\\Browser\\TorBrowser\\Data\\Browser\\profile.default')

# cap = DesiredCapabilities().FIREFOX
# cap["marionette"] = False
# driver = webdriver.Firefox(firefox_binary= binary, executable_path="C:\\Python\\scrapy-master\\Projects\\kgooglecom\\WithScreenshot\\geckodriver.exe")

driver = webdriver.Firefox(firefox_profile= profile, firefox_binary= binary, executable_path="C:\\Python\\scrapy-master\\Projects\\kgooglecom\\WithScreenshot\\geckodriver.exe")
# driver = webdriver.Firefox(firefox_profile=profile, firefox_binary= binary, executable_path=r"C:\\Python\\scrapy-master\\Projects\\kgooglecom\\WithScreenshot\\geckodriver.exe")
driver.get("https://www.google.com")
driver.save_screenshot("screenshot1.png")
driver.quit()

# End :Code For TOR Browser

I Use this link to download the geckodriver

Error:

enter image description here

Please suggest me how to resolve this error

1条回答
时光不老,我们不散
2楼-- · 2019-08-16 20:45

To open a webpage through Tor Browser using Python you can use the Firefox Profile and the tor daemon and you can use the following solution:

  • Code Block:

    from selenium import webdriver
    from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
    import os
    
    torexe = os.popen(r'C:\Users\AtechM_03\Desktop\Tor Browser\Browser\TorBrowser\Tor\tor.exe')
    profile = FirefoxProfile(r'C:\Users\AtechM_03\Desktop\Tor Browser\Browser\TorBrowser\Data\Browser\profile.default')
    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)
    profile.set_preference("network.proxy.socks_remote_dns", False)
    profile.update_preferences()
    driver = webdriver.Firefox(firefox_profile= profile, executable_path=r'C:\Utility\BrowserDrivers\geckodriver.exe')
    driver.get("http://check.torproject.org")
    
  • Browser Snapshot:

Tor_Browser

查看更多
登录 后发表回答