Api To Csv File Conversion In Machine Learning
API TO CSV FILE IN 6 STEPS IN MACHINE LEARNING
Table of contents
hey folks hope you are all doing great so from this article i will try to introduce you all how to fetch data from api in machine learning .
Prerequisite for this article
- you know basic about api
- how api works
- api key to fetch data
- basis about json , how json is useful
for this article i wil be using tmdb site which show popular movies from hollywood,bollywood and other all around the world.
STEP 1: import libraries
import pandas as pd
import requests
STEP 2: SENDING REQUEST FOR SITE TO FETCH DATA USING API KEY
response=requests.get('https://api.themoviedb.org/3/movie/top_rated?api_key=&language=en-US&page=1')
STEP 3: CONVERT JSON INTO READABLE DATA IN ML
data=pd.DataFrame(response.json()['results'])[['id','title','popularity','overview','vote_average','vote_count','release_date']]
data
STEP 4 : CREATE ANOTHER DUMPY DATA FRAME TO GET WHOLE DATA
df=pd.DataFrame()
STEP 5: FETCH ALL PAGE DATA FROM API
for i in range(1,501):
response=requests.get('https://api.themoviedb.org/3/movie/top_rated?api_key=c4240d67d1a5666f710c870a14552172&language=en-US&page={}'.format(i))
data=pd.DataFrame(response.json()['results'])[['id','title','popularity','overview','vote_average','vote_count','release_date']]
df=df.append(data,ignore_index=True)
STEP 6 : CONVERT API DATA TO CSV FILE
df.to_csv('top_rated_movies.csv')
after writing this code you will get .csv file in your folder either in google collab or in jupyternotebook from their you can download it and upload it to the kaggle and boost your kaggle profile .