-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdirectors.py
46 lines (38 loc) · 1.56 KB
/
directors.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from bs4 import BeautifulSoup
import requests
from webscraper import film_urls
films = film_urls()
directors = {}
total_films: int = len(films)
for f in films:
try:
url = 'https://letterboxd.com' + f + 'crew/'
response = requests.get(url, timeout=5)
content = BeautifulSoup(response.content, 'lxml')
director = content.find('span', {'class':'prettify'}).text
# TESTING
# print(director)
if(director in directors):
directors[director] += 1
else:
directors[director] = 1
except:
print(f"no director at {url}")
max_director: list = list()
num_films_directed = directors.values()
max_value = max(num_films_directed)
for key in directors:
if(directors[key] == max_value):
max_director.append(key)
if(len(max_director) == 1):
print(f"\nYour most watched director is {max_director[0]}!\n")
print(f"You have logged {directors[max_director[0]]} films directed by {max_director[0]}, which is {((max_value/total_films) * 100):.2f}% of the films you have logged.\n")
else:
print("\nYour most watched directors are: ")
for d in max_director:
print(d + ', ', end='')
print("and... that's it!\n", end='')
print(f"\nYou have logged {max_value} films from each of these directors, which makes up {((max_value*len(max_director)/total_films)*100):.2f}% of the films you have logged.\n")
# User Martin_Blanke
# Your most watched director is Alfred Hitchcock!
# You have seen 19 films directed by Alfred Hitchcock, which is 3.80% of the films you have logged.