【问题标题】:How to deploy Selenium-python on Heroku如何在 Heroku 上部署 Selenium-python
【发布时间】:2018-03-04 13:11:09
【问题描述】:

所以我试图在 heroku 上部署我使用 node.js 和 python 的应用程序。它可以在我的计算机上运行,​​但是当我尝试在 heroku 上运行它时出现错误:

    from selenium import webdriver
ImportError: no module named selenium

我已将 Chrome、chromedriver 和 selenium 添加为 buildpack,在我的 Procfile 中我什至有:

worker: pip install selenium
worker: python scraper.py

我目前只是想让我的 python 工作,因为我已经确认我的 javascript 工作正常。

【问题讨论】:

    标签: javascript python selenium heroku webdriver


    【解决方案1】:

    模块依赖项不在procfile 中,而是在项目根目录下的requirement.txt 文件中。

    在 Heroku 上部署时,您应该会看到已安装模块的日志。

    此外,除非您在 Heroku 上运行 Chrome Headless,否则您可能不想使用 Chromedriver,因为 Heroku 无法在服务器上打开浏览器:它没有图形界面。

    您可能想要使用 PhantomJS 或 Chrome Headless 之类的东西来完成这项工作。

    【讨论】:

    • 我在 requirements.txt 中有它,我从 Procfile 中取出了它,但我想我只是要重写它以与 heroku 更兼容。 :/我也在无头运行它。唯一的问题是无法安装 selenium 以在 heroku 上的 python 中使用。
    【解决方案2】:

    我在网上搜索后发现的最佳方法是使用来自seleniumPhantomJs() 网络驱动程序

    from selenium import webdriver
    driver = webdriver.PhantomJS()
    
    #your code here
    
    driver.quit()
    

    然后使用这个 buildpackge https://github.com/stomita/heroku-buildpack-phantomjs

    $ heroku create --stack cedar --buildpack https://github.com/stomita/heroku-buildpack-phantomjs.git
    
    # or if your app is already created:
    $ heroku buildpacks:add https://github.com/stomita/heroku-buildpack-phantomjs
    
    $ git push heroku master
    

    它会为你工作:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-01
      • 2018-07-03
      • 2020-06-13
      • 1970-01-01
      • 2013-12-09
      • 1970-01-01
      • 1970-01-01
      • 2015-09-29
      相关资源
      最近更新 更多