Skip to content

Commit 51b7690

Browse files
Create get_imdb_top_250_movies_csv.py (#1659)
* Create get_imdb_top_250_movies_csv.py * Update get_imdb_top_250_movies_csv.py * Update get_imdb_top_250_movies_csv.py * get_imdb_top_250_movies() Co-authored-by: Christian Clauss <[email protected]>
1 parent b212a59 commit 51b7690

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import csv
2+
from typing import Dict
3+
4+
import requests
5+
from bs4 import BeautifulSoup
6+
7+
8+
def get_imdb_top_250_movies(url: str = "") -> Dict[str, float]:
9+
url = url or "https://www.imdb.com/chart/top/?ref_=nv_mv_250"
10+
soup = BeautifulSoup(requests.get(url).text, "html.parser")
11+
titles = soup.find_all("td", attrs="titleColumn")
12+
ratings = soup.find_all("td", class_="ratingColumn imdbRating")
13+
return {
14+
title.a.text: float(rating.strong.text)
15+
for title, rating in zip(titles, ratings)
16+
}
17+
18+
19+
def write_movies(filename: str = "IMDb_Top_250_Movies.csv") -> None:
20+
movies = get_imdb_top_250_movies()
21+
with open(filename, "w", newline="") as out_file:
22+
writer = csv.writer(out_file)
23+
writer.writerow(["Movie title", "IMDb rating"])
24+
for title, rating in movies.items():
25+
writer.writerow([title, rating])
26+
27+
28+
if __name__ == "__main__":
29+
write_movies()

0 commit comments

Comments
 (0)