我想使用 PhantomJS在 Python .我用谷歌搜索了这个问题,但找不到合适的解决方案。
我发现 os.popen()
可能是一个不错的选择。但我无法向它传递一些参数。
目前使用 subprocess.Popen()
可能是一个合适的解决方案。我想知道是否有更好的解决方案。
有没有办法在 Python 中使用 PhantomJS?
最佳答案
在 python 中使用 PhantomJS 最简单的方法是通过 Selenium。最简单的安装方法是
- 安装 NodeJS
- 使用 Node 的包管理器安装 phantomjs:
npm -g install phantomjs-prebuilt
- 安装 selenium(在你的 virtualenv 中,如果你正在使用它)
安装后,你可以使用phantom,简单如下:
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()
的参数。替换这个:
driver = webdriver.PhantomJS() # or add to your PATH
...带有以下内容:
driver = webdriver.PhantomJS(executable_path='/usr/local/lib/node_modules/phantomjs/lib/phantom/bin/phantomjs')
引用资料:
关于python - 有没有办法在 Python 中使用 PhantomJS?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13287490/