w3resource

Python Projects: Display and write the title and rating of Imdb top 250 movies in a csv file

Python Web Project-11 with Solution

Create a Python project to display and write the title and rating of Imdb top 250 movies in a csv file.

Details of top rated movies:
https://www.imdb.com/chart/top

Sample Output:

Movie Title and Rating:

The Shawshank Redemption   9.2
The Godfather   9.1
The Godfather: Part II   9.0
The Dark Knight   9.0
12 Angry Men   8.9
Schindler's List   8.9
The Lord of the Rings: The Return of the King   8.9
Pulp Fiction   8.8
Il buono, il brutto, il cattivo   8.8
The Lord of the Rings: The Fellowship of the Ring   8.8
Fight Club   8.8
Forrest Gump   8.8
Inception   8.7
The Lord of the Rings: The Two Towers   8.7
Star Wars: Episode V - The Empire Strikes Back   8.7
The Matrix   8.6
Goodfellas   8.6
One Flew Over the Cuckoo's Nest   8.6
Shichinin no samurai   8.6
Se7en   8.6
La vita è bella   8.6
Cidade de Deus   8.6
The Silence of the Lambs   8.6
It's a Wonderful Life   8.6
Star Wars   8.6
Saving Private Ryan   8.6
The Green Mile   8.5
Sen to Chihiro no kamikakushi   8.5
Interstellar   8.5
Gisaengchung   8.5
Léon   8.5
Seppuku   8.5
The Usual Suspects   8.5
The Lion King   8.5
The Pianist   8.5
Back to the Future   8.5
Terminator 2: Judgment Day   8.5
American History X   8.5
Modern Times   8.5
Psycho   8.5
Gladiator   8.5
City Lights   8.5
The Departed   8.5
The Intouchables   8.5
Whiplash   8.5
Hotaru no haka   8.5
The Prestige   8.5
Once Upon a Time in the West   8.4
Casablanca   8.4
Nuovo Cinema Paradiso   8.4
Rear Window   8.4
Alien   8.4
Apocalypse Now   8.4
Memento   8.4
Hamilton   8.4
The Great Dictator   8.4
Raiders of the Lost Ark   8.4
Django Unchained   8.4
The Lives of Others   8.4
Joker   8.4
Paths of Glory   8.4
WALL•E   8.4
The Shining   8.4
Avengers: Infinity War   8.4
Sunset Blvd.   8.4
Witness for the Prosecution   8.4
Oldeuboi   8.3
Mononoke-hime   8.3
Spider-Man: Into the Spider-Verse   8.3
Dr. Strangelove or: How I Learned to Stop Worrying and Love the Bomb   8.3
The Dark Knight Rises   8.3
Once Upon a Time in America   8.3
Aliens   8.3
Kimi no na wa.   8.3
Coco   8.3
Avengers: Endgame   8.3
American Beauty   8.3
Braveheart   8.3
Das Boot   8.3
Capharnaüm   8.3
3 Idiots   8.3
Toy Story   8.3
Tengoku to jigoku   8.3
Amadeus   8.3
Inglourious Basterds   8.3
Star Wars: Episode VI - Return of the Jedi   8.3
Good Will Hunting   8.3
Taare Zameen Par   8.3
Reservoir Dogs   8.3
2001: A Space Odyssey   8.3
Requiem for a Dream   8.3
Vertigo   8.3
M - Eine Stadt sucht einen Mörder   8.3
Jagten   8.3
Eternal Sunshine of the Spotless Mind   8.3
Citizen Kane   8.3
Dangal   8.3
Full Metal Jacket   8.2
Ladri di biciclette   8.2
Singin' in the Rain   8.2
The Kid   8.2
Snatch   8.2
North by Northwest   8.2
A Clockwork Orange   8.2
1917   8.2
Scarface   8.2
Idi i smotri   8.2
Ikiru   8.2
Taxi Driver   8.2
Lawrence of Arabia   8.2
Toy Story 3   8.2
Amélie   8.2
Jodaeiye Nader az Simin   8.2
The Sting   8.2
Incendies   8.2
Metropolis   8.2
Per qualche dollaro in più   8.2
The Apartment   8.2
Double Indemnity   8.2
To Kill a Mockingbird   8.2
Up   8.2
Indiana Jones and the Last Crusade   8.2
Heat   8.2
L.A. Confidential   8.2
Die Hard   8.2
Green Book   8.2
Monty Python and the Holy Grail   8.2
Yôjinbô   8.2
Batman Begins   8.2
Rashômon   8.2
Der Untergang   8.2
Bacheha-Ye aseman   8.2
Unforgiven   8.2
Ran   8.2
Some Like It Hot   8.2
Hauru no ugoku shiro   8.2
All About Eve   8.2
A Beautiful Mind   8.2
Casino   8.2
The Great Escape   8.2
The Wolf of Wall Street   8.2
Pan's Labyrinth   8.2
El secreto de sus ojos   8.1
There Will Be Blood   8.1
Lock, Stock and Two Smoking Barrels   8.1
Tonari no Totoro   8.1
Raging Bull   8.1
Judgment at Nuremberg   8.1
The Treasure of the Sierra Madre   8.1
Dial M for Murder   8.1
Three Billboards Outside Ebbing, Missouri   8.1
Shutter Island   8.1
The Gold Rush   8.1
Chinatown   8.1
Babam ve Oglum   8.1
No Country for Old Men   8.1
V for Vendetta   8.1
Inside Out   8.1
Det sjunde inseglet   8.1
The Elephant Man   8.1
The Thing   8.1
Warrior   8.1
The Sixth Sense   8.1
Trainspotting   8.1
Jurassic Park   8.1
Klaus   8.1
The Truman Show   8.1
Gone with the Wind   8.1
Finding Nemo   8.1
Smultronstället   8.1

Blade Runner   8.1
Stalker   8.1
Kill Bill: Vol. 1   8.1
Salinui chueok   8.1
The Bridge on the River Kwai   8.1
Fargo   8.1
Room   8.1
The Third Man   8.1
Relatos salvajes   8.1
Gran Torino   8.1
Tôkyô monogatari   8.1
On the Waterfront   8.1
Anand   8.1
The Deer Hunter   8.1
In the Name of the Father   8.1
Mary and Max   8.1
The Grand Budapest Hotel   8.1
Gone Girl   8.1
Before Sunrise   8.1
Catch Me If You Can   8.1
Hacksaw Ridge   8.1
Höstsonaten   8.1
Persona   8.1
Andhadhun   8.1
Prisoners   8.1
The Big Lebowski   8.1
Sherlock Jr.   8.1
Soul   8.1
To Be or Not to Be   8.1
The General   8.1
How to Train Your Dragon   8.1
Ford v Ferrari   8.1
Barry Lyndon   8.1
12 Years a Slave   8.1
Eskiya   8.1
Mr. Smith Goes to Washington   8.1
Mad Max: Fury Road   8.1
Million Dollar Baby   8.1
Dead Poets Society   8.1
Network   8.1
Stand by Me   8.1
Harry Potter and the Deathly Hallows: Part 2   8.1
Ben-Hur   8.1
Cool Hand Luke   8.1
Hachi: A Dog's Tale   8.1
Ah-ga-ssi   8.1
Platoon   8.0
Logan   8.0
Into the Wild   8.0
Rush   8.0
Le salaire de la peur   8.0
Life of Brian   8.0
Dom za vesanje   8.0
La haine   8.0
Les quatre cents coups   8.0
La passion de Jeanne d'Arc   8.0
Spotlight   8.0
Hotel Rwanda   8.0
Amores perros   8.0
Gangs of Wasseypur   8.0
Andrei Rublev   8.0
Rocky   8.0
Monsters, Inc.   8.0
Kaze no tani no Naushika   8.0
Rebecca   8.0
Du rififi chez les hommes   8.0
Rang De Basanti   8.0
Before Sunset   8.0
Fa yeung nin wah   8.0
Paris, Texas   8.0
Portrait de la jeune fille en feu   8.0
It Happened One Night   8.0
Contratiempo   8.0
Drishyam   8.0
The Help   8.0
La battaglia di Algeri   8.0
Mandariinid   8.0
Koe no katachi   8.0
The Princess Bride   8.0
Trois couleurs: Rouge   8.0

Sample Solution:

Python Code:

#Source https://bit.ly/3t2rjg3
from __future__ import annotations
import csv
import requests
from bs4 import BeautifulSoup

def get_imdb_top_250_movies(url: str = "") -> dict[str, float]:
    url = url or "https://www.imdb.com/chart/top/?ref_=nv_mv_250"
    soup = BeautifulSoup(requests.get(url).text, "html.parser")
    titles = soup.find_all("td", attrs="titleColumn")
    ratings = soup.find_all("td", class_="ratingColumn imdbRating")
    return {
        title.a.text: float(rating.strong.text)
        for title, rating in zip(titles, ratings)
    }
def write_movies(filename: str = "IMDb_Top_250_Movies.csv") -> None:
    movies = get_imdb_top_250_movies()
    print("Movie Title and Rating:\n")
    with open(filename, "w", newline="") as out_file:
        writer = csv.writer(out_file)
        writer.writerow(["Movie title", "IMDb rating"])
        for title, rating in movies.items():
            print(title," ",rating)
            writer.writerow([title, rating])
if __name__ == "__main__":
    write_movies()

Flowchart:

Flowchart: Display and write the title and rating of Imdb top 250 movies in a csv file.

 

Improve this sample solutions and post your code through Disqus



Follow us on Facebook and Twitter for latest update.