简体   繁体   English

如何使用 Python 自动打开和关闭 Tor 浏览器

[英]How to open and close Tor browser automatically with Python

I am playing around with web scraping and Tor.我正在玩网络抓取和 Tor。

I managed to make it work with both requests and Selenium + PhantomJS.我设法使它同时适用于requestsSelenium + PhantomJS。 However, I need that the Tor browser is opened for the script to work.但是,我需要打开 Tor 浏览器以使脚本正常工作。

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.即:自动打开 Tor 浏览器,运行一些脚本,最后自动关闭浏览器。 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.编辑:我在 Mac 上使用 Python 3。

This worked for me:这对我有用:

from selenium import webdriver
import os
import subprocess
#start Tor
sproc=subprocess.Popen('"C:\\Users\\My name\\Desktop\\Tor Browser\\Browser\\firefox.exe"' )

#start PhantomJS
service_args = [ '--proxy=localhost:9150', '--proxy-type=socks5', ]
driver = webdriver.PhantomJS(service_args=service_args)
#get page
driver.get("https://stackoverflow.com/questions/40161921/how-to-open-and-close-tor-browser-automatically-with-python")
print(driver.page_source)
driver.close()
#kill process
sproc.kill()

I think you should add some time pauses between commands:我认为您应该在命令之间添加一些时间暂停:

import time
time.sleep(20)# wait 20 seconds 

Another way to open Tor:打开 Tor 的另一种方法:

os.system('"C:\\Users\\My Name\\Desktop\\Tor Browser\\Browser\\firefox.exe"' )

But this time your command will wait until the called process stops himself (may be user will close it).但是这次您的命令将等到被调用的进程自行停止(可能是用户将关闭它)。 According to your question it is not what you want.根据你的问题,这不是你想要的。 To control executing process let it runs and use special variable to kill it whenever you want.要控制执行过程,让它运行并使用特殊变量随时终止它。

Also pay attention to string path: double quotes inside single quotes.还要注意字符串路径:单引号内的双引号。 There are other ways to pass strings with spaces to system commands, for example: running an outside program (executable) in python?还有其他方法可以将带空格的字符串传递给系统命令,例如: 在 python 中运行外部程序(可执行)? . .

Try this in jupyter:在 jupyter 中试试这个:

import webbrowser
urL='https://YOUR WEBSITE ADDRESS HERE'
mozilla_path="C:\\Users\\T14s\\Desktop\\Tor Browser\\Browser\\firefox.exe"
webbrowser.register('firefox', None,webbrowser.BackgroundBrowser(mozilla_path))
webbrowser.get('firefox').open_new_tab(urL)
import os
import time
time.sleep(10)
os.system("taskkill /im firefox.exe /f")

TOR is based on firefox - hence firefox comes up a lot. TOR 基于firefox - 因此firefox 出现了很多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM