I am playing around with web scraping and Tor.
I managed to make it work with both requests
and Selenium
+ PhantomJS. However, I need that the Tor browser is opened for the script to work.
This is why I am trying now to automatise the complete process; that is: open Tor browser automatically, run some script and at the end close the browser automatically. But I am struggling with it.
#open Tor browser
os.system('open /Applications/TorBrowser.app')
#code to scrape
#close Tor browser
???
Open
To open the browser, some other options I found out there are not working.
import subprocess
subprocess.Popen('/Applications/TorBrowser.app') #permission denied
or
os.system('start /Applications/TorBrowser.app') #sh: start: command not found
However, the following line worked:
os.system('open /Applications/TorBrowser.app')
Close
The main problem is to close the browser afterwards, as none of the commands found in other posts worked.
Those include:
os.system("taskkill /im /Applications/TorBrowser.app /f") #sh: taskkill: command not found
or
os.system("kill /Applications/TorBrowser.app") #sh: line 0: kill: /Applications/TorBrowser.app: arguments must be process or job IDs
or
os.close('/Applications/TorBrowser.app') #TypeError: an integer is required (got type str)
Any suggestions of how to close it?
And is there a better way to open it?
Edit: I'm on Mac with Python 3.