After trying so many solutions out there, i finally managed to opened up Tor browser, by this on my windows machine:

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

binary = FirefoxBinary(r"C:\Program Files\Tor Browser\Browser\firefox.exe")
profile = webdriver.FirefoxProfile("C:\Program Files\Tor Browser\Browser\TorBrowser\Data\Browser\profile.default")
profile.set_preference('network.proxy.type', 1)
profile.set_preference('network.proxy.socks', '')
profile.set_preference('network.proxy.socks_port', 9050)

driver = webdriver.Firefox(executable_path=r"geckodriver.exe", firefox_binary=binary)

though it's still not completly automated, as i have to manually start the Tor browser first to make this work.

Now the challenged is to automatically generate new identity in Tor, for this i managed to get another solution that works for me, like this:

import requests
from stem import Signal
from stem.control import Controller

def get_tor_session():
    session = requests.session()
    session.proxies = {'http':  'socks5://', 'https': 'socks5://'}
    return session

def renew_connection():
    with Controller.from_port(port = 9051) as controller:

session = get_tor_session()
print("Tor IP: ",session.get("").text)
print("Primary Ip: ",requests.get("").text)

session = get_tor_session()
print("Tor New Ip: ",session.get("").text)

here it uses Tor Bundle, and also have to manually run tor.exe file from the tor bundle folder first, then it works.

i want to generate new identity using selenium, as i want to scrape dynamic webpages, that have javascripts, clicking buttons etc, and as per my knowledge i think that is not possible using requests library!

any suggestion is highly appreciated!

0 Answers