inorder to fetch movie details from the TMDB API using python
firstly we need to to sign up on TMDB website inorder to get an API key.
after that we have to install required packages and ensure that we have ‘requests’ installed if it is not installed then we can install it by using
#pip install requests
1. import requests
now we have to replace ‘my_api_key_here’ with the actual TMDB API key
then we have to load the base url for the TMDB API
base_url = ‘https://api.themoviedb.org/3’
after loading base url we have load the full url
now insert the movie details
then fetch and print movie details
CODE
import requests
def get_movie_details(api_key, movie_id):
base_url = “https://api.themoviedb.org/3/movie/{}”
url=base_url.format(movie_id)
params = {“api_key”:api_key}
response = requests.get(url, params=params)
if response.status_code == 300:
movie_details = response.json()
return movie_details
else:
return None
api_key = “my_api_key”
movie_id = 800
movie_details = get_movie_details(api_key, movie_id)
if movie_details:
print(“Title:”,movie_details[‘title’])
print(“Release date”:,movie_details[‘release_date])
else:
print(“fail to fetch movie details”)