我有一个在 Heroku 上运行的 Node 应用程序。我正在使用 python 中的 selenium 抓取一个网站,并在需要时从我的 Node 应用程序调用 python 脚本。我在我的 mac 上安装了 PhantomJS,当我在本地运行应用程序时(node index.js),一切正常。
path_to_phantom = '/Users/govind/Desktop/phantomjs-2.1.1-
macosx/bin/phantomjs'
browser = webdriver.PhantomJS(executable_path = path_to_phantom)
但是,似乎在 Heroku 上没有任何效果。我还添加了 PhantomJS buildpack到我的 Node 应用程序,但它只是不调用 python 脚本。我认为问题是 PhantomJS buildpack 的路径。我应该添加什么路径?还是我在这里遗漏了任何其他方面?
请您参考如下方法:
我按照以下步骤在部署到 Heroku 的 Python 应用程序中成功地将 Selenium 与 PhantomJS 结合使用:
1) 在我的 Heroku 应用程序上切换到使用 Cedar-14 堆栈
$ heroku stack:set cedar-14
2) 安装 PhantomJS buildpack
$ heroku buildpacks:add https://github.com/stomita/heroku-buildpack-phantomjs
通过这些更改,我可以使用 Selenium 来获取网站
from selenium import webdriver
browser = webdriver.PhantomJS()
browser.get("http://www.google.com") # This does not throw an exception if it got a 404
html = browser.page_source
print html # If this outputs more than just '<html><head></head><body></body></html>' you know that it worked
