Step 1: Set Up Your Code
Add the following lines to your code before you use Selenium.
from selenium import webdriver
import os
chrome_options = webdriver.ChromeOptions()
chrome_options.binary_location = os.environ.get("GOOGLE_CHROME_BIN")
chrome_options.add_argument("--headless")
chrome_options.add_argument("--disable-dev-shm-usage")
chrome_options.add_argument("--no-sandbox")
driver = webdriver.Chrome(executable_path=os.environ.get("CHROMEDRIVER_PATH"), chrome_options=chrome_options)
# Now you can start using Selenium
Step 2: Add the Buildpacks
On Heroku, open your App. Click on the Settings tab and scroll down to Buildpacks. Add the following:
- Python (Select it from the officially supported buildpacks)
- Headless Google Chrome: https://github.com/heroku/heroku-buildpack-google-chrome
- Chromedriver: https://github.com/heroku/heroku-buildpack-chromedriver
Step 3: Add the Config Vars
Scroll to the config vars section. Here, we will add the paths to Chrome and the Chromedriver. Add the following config vars:
- CHROMEDRIVER_PATH = /app/.chromedriver/bin/chromedriver
- GOOGLE_CHROME_BIN = /app/.apt/usr/bin/google-chrome
Step 4: Deploy the Application
If everything worked out correctly, then your application should be ready to deploy!
https://www.andressevilla.com/running-chromedriver-with-python-selenium-on-heroku/
How to Publish your API on Heroku
1. Install Heroku
First, we’ll need to register for a Heroku account and install the Heroku Command Line Interface (CLI) with the following command (works with Ubuntu 16+):
Then log in to the Heroku CLI:
2. Add the Necessary Files
Now, let’s add the necessary files for publication to the folder within our app:
- requirements.txt with a list of necessary Python modules
- Procfile, which explicitly declares what commands should be executed to start our app
- .gitignore, to exclude files we don’t need on the server
Our requirements.txt file will contain the following lines:
Please note that we have added gunicorn
(Python WSGI HTTP Server) to our list, since it is necessary to run our app on the server.
Procfile will contain:
and .gitignore will contain the following:
Now that we’ve created the necessary files, let’s initialize a git repository and commit:
3. Create a new Heroku application
And then finally push our master branch to the Heroku remote repository:
Now we can start and open our API Service using the commands:
Our API will now be accessible at: https://your-random-heroku-name.herokuapp.com/ai-quotes/
Nhận xét
Đăng nhận xét