0% found this document useful (0 votes)
9 views

Fetching Data From Api Cheatsheet

The document shows code for scraping movie data from an API, cleaning and formatting it into a Pandas dataframe, and saving it as a CSV file. It imports necessary libraries, makes a GET request to the API to get top rated movie data as JSON, converts the results to a dataframe, and then loops through multiple API pages to append all movie data into one large dataframe with over 8,500 rows. It then checks the dataframe shape and saves it to a CSV file.

Uploaded by

Yajur Agarwal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Fetching Data From Api Cheatsheet

The document shows code for scraping movie data from an API, cleaning and formatting it into a Pandas dataframe, and saving it as a CSV file. It imports necessary libraries, makes a GET request to the API to get top rated movie data as JSON, converts the results to a dataframe, and then loops through multiple API pages to append all movie data into one large dataframe with over 8,500 rows. It then checks the dataframe shape and saves it to a CSV file.

Uploaded by

Yajur Agarwal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

In 

[1]: import pandas as pd


import requests

In [6]: response = requests.get('https://api.themoviedb.org/3/movie/top_rated?api_key=8265bd1679663a7ea12ac168da84d2e8&language=en-US&page=1')


 

In [7]: temp_df = pd.DataFrame(response.json()['results'])[['id','title','overview','release_date','popularity','vote_average','vote_count']]


 

In [34]: df.head()

Out[34]: id title overview release_date popularity vote_average vote_count

0 19404 Dilwale Dulhania Le Jayenge Raj is a rich, carefree, happy-go-lucky second... 1995-10-20 18.433 8.7 2763

1 724089 Gabriel's Inferno Part II Professor Gabriel Emerson finally learns the t... 2020-07-31 8.439 8.7 1223

2 278 The Shawshank Redemption Framed in the 1940s for the double murder of h... 1994-09-23 65.570 8.7 18637

3 238 The Godfather Spanning the years 1945 to 1955, a chronicle o... 1972-03-14 63.277 8.7 14052

4 761053 Gabriel's Inferno Part III The final part of the film adaption of the ero... 2020-11-19 26.691 8.7 773

In [17]: df = pd.DataFrame()

In [18]: df

Out[18]:

In [19]: for i in range(1,429):


response = requests.get('https://api.themoviedb.org/3/movie/top_rated?api_key=8265bd1679663a7ea12ac168da84d2e8&language=en-US&page={}'.format(i))
temp_df = pd.DataFrame(response.json()['results'])[['id','title','overview','release_date','popularity','vote_average','vote_count']]
df = df.append(temp_df,ignore_index=True)

 
In [21]: df

Out[21]: id title overview release_date popularity vote_average vote_count

0 19404 Dilwale Dulhania Le Jayenge Raj is a rich, carefree, happy-go-lucky second... 1995-10-20 18.433 8.7 2763

1 724089 Gabriel's Inferno Part II Professor Gabriel Emerson finally learns the t... 2020-07-31 8.439 8.7 1223

2 278 The Shawshank Redemption Framed in the 1940s for the double murder of h... 1994-09-23 65.570 8.7 18637

3 238 The Godfather Spanning the years 1945 to 1955, a chronicle o... 1972-03-14 63.277 8.7 14052

4 761053 Gabriel's Inferno Part III The final part of the film adaption of the ero... 2020-11-19 26.691 8.7 773

... ... ... ... ... ... ... ...

8546 13805 Disaster Movie The filmmaking team behind the hits "Scary Mov... 2008-08-29 14.630 3.2 714

8547 5491 Battlefield Earth In the year 3000, man is no match for the Psyc... 2000-05-12 10.647 3.0 543

8548 14164 Dragonball Evolution The young warrior Son Goku sets out on a quest... 2009-03-12 32.244 2.8 1447

8549 11059 House of the Dead Set on an island off the coast, a techno rave ... 2003-04-11 14.502 2.8 238

8550 40016 Birdemic: Shock and Terror A platoon of eagles and vultures attacks the r... 2010-02-27 9.824 2.2 215

8551 rows × 7 columns

In [22]: df.shape

Out[22]: (8551, 7)

In [23]: df.to_csv('movies.csv')

In [ ]: ​

You might also like