python - 有没有办法在 Python 中使用 PhantomJS?

标签 python phantomjs

我想使用 PhantomJSPython .我用谷歌搜索了这个问题,但找不到合适的解决方案。

我发现 os.popen() 可能是一个不错的选择。但我无法向它传递一些参数。

目前使用 subprocess.Popen() 可能是一个合适的解决方案。我想知道是否有更好的解决方案。

有没有办法在 Python 中使用 PhantomJS?

最佳答案

在 python 中使用 PhantomJS 最简单的方法是通过 Selenium。最简单的安装方法是

  1. 安装 NodeJS
  2. 使用 Node 的包管理器安装 phantomjs:npm -g install phantomjs-prebuilt
  3. 安装 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/

相关文章:

python - 是否可以加载一次模型并在 python 中再次重用它?

javascript - 无法从 CasperJS 中的 thenEvaluate() 函数内访问变量

带有 phantomjs 的 Meteor Spiderable 不显示产量内容

request - 如何停止/中止/取消 PhantomJS 中的页面加载?

Python NumPy : sum every 3 rows (converting monthly to quarterly)

Python:来自多个线程(或信号)的 Queue.get()

python - sklearn 高斯过程回归器中的优化器调整

javascript - XMLHttpRequest 无法加载 https ://[website]. com 不支持跨源请求

ruby-on-rails - 我可以看看 PhantomJS 中发生了什么吗?

python - 在 For 循环中更新索引值