Introduction
In this tutorial, we will be building a movie scrapper using Python. Next, we will be wrapping it into a web app using Flask. For the movie data we will be scrapping from an API from RAPIDAPI, the link is here. We will also be using the requests library to make HTTP requests.
Table of Contents
Install modules
Now it’s time to install Flask and requests. To install them, run the commands below on your terminal.
pip3 install Flask
pip3 install requests
App structure
Create a folder called flask-movie and inside create a folder called templates with a file called index.html in it which will contain our application user interface. Inside the flask-movie folder create a file called app.py.
Import modules
Inside the app.py file we created earlier let’s import a few modules and initialize our application.
from flask import Flask, request, render_template
import requests
app = Flask(__name__)
.
Scrape movie data
Now let’s create a view to scrape data from an API. In this tutorial, we will be using an API from RAPIDAPI to get our data. Go to RAPIDAPI and subscribe to the API and make sure to keep your API key private.
@app.route('/',methods=['GET','POST'])
def index():
if request.method == "POST":
title = request.form['title']
url = "https://ott-details.p.rapidapi.com/search"
querystring = {"title":title,"page":"1"}
headers = {
"X-RapidAPI-Key": "YOUR API KEY",
"X-RapidAPI-Host": "ott-details.p.rapidapi.com"
}
response = requests.request("GET", url, headers=headers, params=querystring).json()
for data in response['results']:
genre = data['genre']
title = data['title']
typ = data['type']
released = data['released']
return render_template("index.html",genre=genre,title=title,typ=typ, released=released)
return render_template("index.html")
if __name__ == "__main__":
app.run(debug=True)
Run
Now open your terminal and run the command shown below.
python3 app.py
Results
If the command above runs successfully your application should be available at the address [ http://127.0.0.1:5000 ] as shown below.
There you have it. Thanks for reading. Happy Coding.