我使用以下代码在 Python 中启动并关闭 phantomjs
from selenium import webdriver
driver = webdriver.PhantomJS()
driver.get(url)
html_doc = driver.page_source
driver.close()
在脚本结束执行后,我仍然在我的 Mac 事件监视器中找到 phantomjs
的实例。实际上,每次我运行脚本时,都会创建一个新进程 phantomjs
。
我应该如何关闭驱动程序?
最佳答案
截至 2016 年 7 月,driver.close()
和 driver.quit()
对我来说还不够。这杀死了 node
进程,但没有杀死它产生的 phantomjs
子进程。
在 this GitHub issue 的讨论之后,对我有用的单一解决方案是运行:
import signal
driver.service.process.send_signal(signal.SIGTERM) # kill the specific phantomjs child proc
driver.quit() # quit the node proc
关于python - 如何正确停止 phantomjs 执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25110624/