How to Build a Flask Movie Scrapper

How to Build a Flask Movie Scrapper

Introduction

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

@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.

Leave a Comment

Your email address will not be published. Required fields are marked *