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

2024-03-16

我想用PhantomJS http://phantomjs.org/ in Python http://www.python.org/。我用谷歌搜索了这个问题但找不到正确的解决方案。

I find os.popen()可能是一个不错的选择。但我无法向它传递一些论点。

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

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


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

  1. Install NodeJS https://nodejs.org/
  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')

参考:

  • http://selenium-python.readthedocs.io/ http://selenium-python.readthedocs.io/
  • 如何在 python webdriver 中为 phantomjs/ghostdriver 设置代理? https://stackoverflow.com/questions/14699718/how-do-i-set-a-proxy-for-phantomjs-ghostdriver-in-python-webdriver/15699530#15699530
  • https://dzone.com/articles/python-testing-phantomjs https://dzone.com/articles/python-testing-phantomjs
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

有没有办法在Python中使用PhantomJS? 的相关文章

随机推荐