Skip to content

Commit 68794d8

Browse files
committed
Web scraping and putting content in CSV Working
1 parent f9d47b5 commit 68794d8

7 files changed

Lines changed: 75 additions & 52 deletions

File tree

‎.DS_Store‎

0 Bytes
Binary file not shown.

‎projects/.DS_Store‎

0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
Lines changed: 60 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,68 @@
11
from bs4 import BeautifulSoup
22
import requests
33
import json
4+
import pandas as pd
45

56
# Setting up session
67
s = requests.session()
78

89
#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')

‎projects/Find_imdb_rating/movies.txt‎

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Film Name,Rating,Genre
2+
Star Wars: Episode V - The Empire Strikes Back,8.7,"
3+
Action, Adventure, Fantasy "
4+
Star Wars: Episode V - The Empire Strikes Back: Deleted Scenes,8.7,"
5+
Short, Action, Drama "
6+
The Wolf of Wall Street,8.2,"
7+
Biography, Crime, Drama "
8+
The Wolf of Wall Street,6.5,"
9+
Drama "
10+
Shutter Island,8.2,"
11+
Mystery, Thriller "
12+
Shutter Island: Into the Lighthouse,8.8,"
13+
Documentary, Short "
14+
Shutter Island: Behind the Shutters,8.4,"
15+
Documentary, Short "

‎projects/Find_imdb_rating/ratings.csv‎

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)