Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
806 views
in Technique[技术] by (71.8m points)

python 3.x - How to use Tor with Chrome browser through Selenium

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,
)
  1. 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 BrowserBrowserTorBrowserToror.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:

Tor_Chrome


You can find a relevant discussion in How to connect to Tor browser using Python


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...