-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
56 lines (46 loc) · 1.51 KB
/
main.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
47
48
49
50
51
52
53
54
55
56
import requests
import csv
from bs4 import BeautifulSoup as bs
username = input('What is your trakt.tv username? ')
# Generates links based on your username
first_url = 'https://trakt.tv/users/'+ username +'/history/movies/added?genres='
base = 'https://trakt.tv/users/'+ username +'/history/movies/added?genres=&page='
# Gets the page content for the first page
results = requests.get(first_url)
src = results.content
soup = bs(src, 'lxml')
# Finds the number of pages you have
num_pages = soup.find_all(class_='page')
biggest_number = 0
for li in num_pages:
a = li.find('a')
number = a.get_text()
if number != '':
if int(number) > biggest_number:
biggest_number = int(number)
# Writes all the movies from the first page in a list
titles = soup.find_all(class_='titles')
table = []
for title in titles:
h3 = title.find('h3').get_text()
table.append(h3)
i = 2
while i <= biggest_number:
# Gets the page content for the rest of the pages
new_url = base + str(i)
results2 = requests.get(new_url)
src2 = results2.content
soup2 = bs(src2, 'lxml')
# Writes the rest of the movies from all other pages in the same list
titles2 = soup2.find_all(class_='titles')
for title2 in titles2:
h3_2 = title2.find('h3').get_text()
table.append(h3_2)
i += 1
# Removes all the duplicates
new_table = list(dict.fromkeys(table))
# Writes the movies as a .csv file
dat = open("movies.csv", "w")
writer = csv.writer(dat)
writer.writerow(new_table)
dat.close()