mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-05-18 15:11:28 +00:00
* Replace dependency `requests` with `httpx` Fixes #12742 Signed-off-by: Lim, Lukaz Wei Hwang <lukaz.wei.hwang.lim@intel.com> * updating DIRECTORY.md * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Signed-off-by: Lim, Lukaz Wei Hwang <lukaz.wei.hwang.lim@intel.com> Co-authored-by: Lim, Lukaz Wei Hwang <lukaz.wei.hwang.lim@intel.com> Co-authored-by: cclauss <cclauss@users.noreply.github.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
39 lines
1.0 KiB
Python
39 lines
1.0 KiB
Python
# /// script
|
|
# requires-python = ">=3.13"
|
|
# dependencies = [
|
|
# "beautifulsoup4",
|
|
# "httpx",
|
|
# ]
|
|
# ///
|
|
|
|
from __future__ import annotations
|
|
|
|
import csv
|
|
|
|
import httpx
|
|
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(httpx.get(url, timeout=10).text, "html.parser")
|
|
titles = soup.find_all("h3", class_="ipc-title__text")
|
|
ratings = soup.find_all("span", class_="ipc-rating-star--rating")
|
|
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()
|