|
1 | 1 | from bs4 import BeautifulSoup |
2 | 2 | import requests |
3 | 3 | import json |
| 4 | +import pandas as pd |
4 | 5 |
|
5 | 6 | # Setting up session |
6 | 7 | s = requests.session() |
7 | 8 |
|
8 | 9 | #File to write movies into |
9 | | -file1 = open ("ratings.csv", "w") |
10 | | - |
11 | | -with open("movies.txt", "r") as file2: |
12 | | - for line in file2: |
13 | | - x = line.split(", ") |
14 | | - title = x[0].lower() |
15 | | - release = x[1] |
16 | | - query = "+".join(title.split()) |
17 | | - URL = "https://www.imdb.com/search/title/?title=" + query |
18 | | - try: |
19 | | - response = s.get(URL) |
20 | | - |
21 | | - content = response.content |
22 | | - |
23 | | - soup = BeautifulSoup(response.content) |
24 | | - containers = soup.find_all("div", class_="lister-item-content") |
25 | | - for result in containers: |
26 | | - name1 = result.h3.a.text |
27 | | - name = result.h3.a.text.lower() |
28 | | - # print(name) |
29 | | - year = result.h3.find( |
30 | | - "span", class_="lister-item-year text-muted unbold" |
31 | | - ).text.lower() |
32 | | - |
33 | | - # print(name) |
34 | | - # print(year) |
35 | | - # print(release) |
36 | | - |
37 | | - if title in name and release in year: |
38 | | - rating = result.find("div",class_="inline-block ratings-imdb-rating")["data-value"] |
39 | | - print(f"Rating of {name1}:", rating) |
40 | | - genres = [] |
41 | | - genre = result.p.find("span", class_="genre") |
42 | | - for x in genre: |
43 | | - genres.append(x) |
44 | | - file1.write(name1) |
45 | | - file1.write(',') |
46 | | - file1.write(rating) |
47 | | - # file1.write(',') |
48 | | - file1.write('\n') |
49 | | - |
50 | | - |
51 | | - |
52 | | - |
53 | | - except Exception: |
54 | | - print("Try again with valid combination of tile and release year") |
55 | | - |
56 | | -file1.close() |
57 | | - |
| 10 | + |
| 11 | +films = ['Star Wars: Episode V - The Empire Strikes Back', 'The wolf of wall street', 'Shutter Island'] |
| 12 | + |
| 13 | +names = [] |
| 14 | +ratings = [] |
| 15 | +genres = [] |
| 16 | + |
| 17 | +for line in films: |
| 18 | + # x = line.split(", ") |
| 19 | + title = line.lower() |
| 20 | + # release = x[1] |
| 21 | + query = "+".join(title.split()) |
| 22 | + URL = "https://www.imdb.com/search/title/?title=" + query |
| 23 | + print(URL) |
| 24 | + # print(release) |
| 25 | + try: |
| 26 | + response = s.get(URL) |
| 27 | + |
| 28 | + #getting contect from IMDB Website |
| 29 | + content = response.content |
| 30 | + |
| 31 | + # print(response.status_code) |
| 32 | + |
| 33 | + soup = BeautifulSoup(response.content, features="html.parser") |
| 34 | + #searching all films containers found |
| 35 | + containers = soup.find_all("div", class_="lister-item-content") |
| 36 | + for result in containers: |
| 37 | + name1 = result.h3.a.text |
| 38 | + name = result.h3.a.text.lower() |
| 39 | + |
| 40 | + # Uncomment below lines if you want year specific as well, define year variable before this |
| 41 | + # year = result.h3.find( |
| 42 | + # "span", class_="lister-item-year text-muted unbold" |
| 43 | + # ).text.lower() |
| 44 | + |
| 45 | + #if film found (searching using name) |
| 46 | + if title in name: |
| 47 | + #scraping rating |
| 48 | + rating = result.find("div",class_="inline-block ratings-imdb-rating")["data-value"] |
| 49 | + # print(f"Rating of {name1}:", rating) |
| 50 | + #scraping genre |
| 51 | + genre = result.p.find("span", class_="genre") |
| 52 | + genre = genre.contents[0] |
| 53 | + |
| 54 | + #appending name, rating and genre to individual lists |
| 55 | + names.append(name1) |
| 56 | + ratings.append(rating) |
| 57 | + genres.append(genre) |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | + except Exception: |
| 62 | + print("Try again with valid combination of tile and release year") |
| 63 | + |
| 64 | +#storing in pandas dataframe |
| 65 | +df = pd.DataFrame({'Film Name':names,'Rating':ratings,'Genre':genres}) |
| 66 | + |
| 67 | +#making csv using pandas |
| 68 | +df.to_csv('products.csv', index=False, encoding='utf-8') |
0 commit comments