mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-01-30 22:23:42 +00:00
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 <cclauss@me.com>
This commit is contained in:
parent
b212a59754
commit
51b769095f
29
web_programming/get_imdb_top_250_movies_csv.py
Normal file
29
web_programming/get_imdb_top_250_movies_csv.py
Normal file
|
@ -0,0 +1,29 @@
|
|||
import csv
|
||||
from typing import Dict
|
||||
|
||||
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()
|
||||
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():
|
||||
writer.writerow([title, rating])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
write_movies()
|
Loading…
Reference in New Issue
Block a user