|
1 | | -import requests |
2 | 1 | from bs4 import BeautifulSoup |
| 2 | +import requests |
| 3 | +import json |
| 4 | +import pandas as pd |
| 5 | +import os |
| 6 | + |
| 7 | +# Setting up session |
| 8 | +s = requests.session() |
| 9 | + |
| 10 | +# List contaiting all the films for which data has to be scraped from IMDB |
| 11 | +films = [] |
| 12 | + |
| 13 | +# Lists contaiting web scraped data |
| 14 | +names = [] |
| 15 | +ratings = [] |
| 16 | +genres = [] |
| 17 | + |
| 18 | +# Define path where your films are present |
| 19 | +# For eg: "/Users/utkarsh/Desktop/films" |
| 20 | +path = input("Enter the path where your films are: ") |
| 21 | + |
| 22 | +# Films with extensions |
| 23 | +filmswe = os.listdir(path) |
| 24 | + |
| 25 | +for film in filmswe: |
| 26 | + # Append into my films list (without extensions) |
| 27 | + films.append(os.path.splitext(film)[0]) |
| 28 | + # print(os.path.splitext(film)[0]) |
| 29 | + |
| 30 | +for line in films: |
| 31 | + # x = line.split(", ") |
| 32 | + title = line.lower() |
| 33 | + # release = x[1] |
| 34 | + query = "+".join(title.split()) |
| 35 | + URL = "https://www.imdb.com/search/title/?title=" + query |
| 36 | + print(URL) |
| 37 | + # print(release) |
| 38 | + try: |
| 39 | + response = s.get(URL) |
| 40 | + |
| 41 | + #getting contect from IMDB Website |
| 42 | + content = response.content |
| 43 | + |
| 44 | + # print(response.status_code) |
| 45 | + |
| 46 | + soup = BeautifulSoup(response.content, features="html.parser") |
| 47 | + #searching all films containers found |
| 48 | + containers = soup.find_all("div", class_="lister-item-content") |
| 49 | + for result in containers: |
| 50 | + name1 = result.h3.a.text |
| 51 | + name = result.h3.a.text.lower() |
| 52 | + |
| 53 | + # Uncomment below lines if you want year specific as well, define year variable before this |
| 54 | + # year = result.h3.find( |
| 55 | + # "span", class_="lister-item-year text-muted unbold" |
| 56 | + # ).text.lower() |
| 57 | + |
| 58 | + #if film found (searching using name) |
| 59 | + if title in name: |
| 60 | + #scraping rating |
| 61 | + rating = result.find("div",class_="inline-block ratings-imdb-rating")["data-value"] |
| 62 | + #scraping genre |
| 63 | + genre = result.p.find("span", class_="genre") |
| 64 | + genre = genre.contents[0] |
| 65 | + |
| 66 | + #appending name, rating and genre to individual lists |
| 67 | + names.append(name1) |
| 68 | + ratings.append(rating) |
| 69 | + genres.append(genre) |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | + except Exception: |
| 74 | + print("Try again with valid combination of tile and release year") |
| 75 | + |
| 76 | +#storing in pandas dataframe |
| 77 | +df = pd.DataFrame({'Film Name':names,'Rating':ratings,'Genre':genres}) |
3 | 78 |
|
4 | | -# Defining variables and url |
5 | | -title = str(input("Enter the title of movie/series: ")).lower() |
6 | | -release = str(input("Enter the year of release: ")).lower() |
7 | | -query = "+".join(title.split()) |
8 | | -URL = f"https://www.imdb.com/search/title/?title={query}" |
9 | | - |
10 | | -s = requests.session() # Setting up session |
11 | | - |
12 | | -try: |
13 | | - response = s.get(URL) |
14 | | - soup = BeautifulSoup(response.content, features="lxml") |
15 | | - containers = soup.find_all("div", class_="lister-item-content") |
16 | | - |
17 | | - for result in containers: |
18 | | - name = result.h3.a.text.lower() |
19 | | - year = result.h3.find( |
20 | | - "span", class_="lister-item-year text-muted unbold" |
21 | | - ).text.lower() |
22 | | - |
23 | | - if title in name and release in year: |
24 | | - rating = result.find("div", |
25 | | - class_="inline-block ratings-imdb-rating")[ |
26 | | - "data-value"] |
27 | | - print(f"Rating of {name}:", rating) |
28 | | -except Exception: |
29 | | - print("Try again with valid combination of tile and release year") |
| 79 | +#making csv using pandas |
| 80 | +df.to_csv('film_ratings.csv', index=False, encoding='utf-8') |
0 commit comments