问题描述
我想在 Python 中使用 PhantomJS .我用谷歌搜索了这个问题,但是找不到合适的解决方案.
I want to use PhantomJS in Python. I googled this problem but couldn't find proper solutions.
我发现os.popen()
可能是一个不错的选择.但是我无法通过一些争论.
I find os.popen()
may be a good choice. But I couldn't pass some arguments to it.
使用subprocess.Popen()
可能是目前合适的解决方案.我想知道是否有更好的解决方案.
Using subprocess.Popen()
may be a proper solution for now. I want to know whether there's a better solution or not.
是否可以在Python中使用PhantomJS?
Is there a way to use PhantomJS in Python?
推荐答案
在python中使用PhantomJS的最简单方法是通过Selenium.最简单的安装方法是
The easiest way to use PhantomJS in python is via Selenium. The simplest installation method is
- 安装 NodeJS
- 使用Node的程序包管理器安装phantomjs:
npm -g install phantomjs-prebuilt
- 安装硒(如果正在使用,请在您的virtualenv中安装)
- Install NodeJS
- Using Node's package manager install phantomjs:
npm -g install phantomjs-prebuilt
- install selenium (in your virtualenv, if you are using that)
安装后,您可以简单地使用幻像:
After installation, you may use phantom as simple as:
from selenium import webdriver
driver = webdriver.PhantomJS() # or add to your PATH
driver.set_window_size(1024, 768) # optional
driver.get('https://google.com/')
driver.save_screenshot('screen.png') # save a screenshot to disk
sbtn = driver.find_element_by_css_selector('button.gbqfba')
sbtn.click()
如果系统路径环境变量设置不正确,则需要指定确切的路径作为webdriver.PhantomJS()
的参数.替换为:
If your system path environment variable isn't set correctly, you'll need to specify the exact path as an argument to webdriver.PhantomJS()
. Replace this:
driver = webdriver.PhantomJS() # or add to your PATH
...具有以下内容:
... with the following:
driver = webdriver.PhantomJS(executable_path='/usr/local/lib/node_modules/phantomjs/lib/phantom/bin/phantomjs')
参考文献:
- http://selenium-python.readthedocs.io/
- 如何在python webdriver中为phantomjs/ghostdriver设置代理?
- https://dzone.com/articles/python-testing-phantomjs
- http://selenium-python.readthedocs.io/
- How do I set a proxy for phantomjs/ghostdriver in python webdriver?
- https://dzone.com/articles/python-testing-phantomjs
这篇关于有没有办法在Python中使用PhantomJS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!