Skip to content

Commit b637d28

Browse files
committed
Web scraping working
1 parent 5475eb7 commit b637d28

5 files changed

Lines changed: 90 additions & 13 deletions

File tree

6 KB
Binary file not shown.
Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,50 @@
1+
from bs4 import BeautifulSoup
12
import requests
23
import json
34

4-
# Defining variables and url
5-
title = str(input("Enter the title of movie/series: "))
6-
year = str(input("Enter the year of release: "))
7-
query = "+".join(title.split())
5+
# Setting up session
6+
s = requests.session()
87

9-
request_link = 'http://www.omdbapi.com/?i=tt3896198&apikey=2c84f11f'
8+
#File to write movies into
9+
file1 = open ("ratings.csv", "w")
1010

11-
#final request link using OMDb API
12-
request = request_link + '&t=' + query + '&y=' + year
11+
with open("movies.txt", "r") as file2:
12+
for line in file2:
13+
title = line.lower()
14+
query = "+".join(title.split())
15+
URL = "https://www.imdb.com/search/title/?title=" + query
16+
try:
17+
response = s.get(URL)
1318

14-
#requesting data from OMBd
15-
response = requests.get(request)
19+
content = response.content
1620

17-
#converting to json (dictionary)
18-
response = response.json()
21+
soup = BeautifulSoup(response.content)
22+
containers = soup.find_all("div", class_="lister-item-content")
23+
for result in containers:
24+
name1 = result.h3.a.text
25+
name = result.h3.a.text.lower()
26+
# print(name)
27+
# year = result.h3.find(
28+
# "span", class_="lister-item-year text-muted unbold"
29+
# ).text.lower()
1930

20-
#printing imdb rating
21-
print(response['imdbRating'])
31+
if title in name:
32+
rating = result.find("div",class_="inline-block ratings-imdb-rating")["data-value"]
33+
print(f"Rating of {name1}:", rating)
34+
genre = result.p.find("span", class_="genre")
35+
file1.write(name1)
36+
file1.write(',')
37+
file1.write(rating)
38+
file1.write(',')
39+
file1.write(genre)
40+
file1.write('\n')
41+
42+
# for x in genre:
43+
# print(x)
44+
45+
46+
except Exception:
47+
print("Try again with valid combination of tile and release year")
48+
49+
file1.close()
50+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Iron Man
2+
Star Wars
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Star Wars: Episode IX - The Rise of Skywalker,6.6,
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from bs4 import BeautifulSoup
2+
import requests
3+
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 = "https://www.imdb.com/search/title/?title=" + query
9+
10+
print(URL)
11+
12+
s = requests.session() # Setting up session
13+
14+
file1 = open ("ratings.csv", "a")
15+
16+
try:
17+
response = s.get(URL)
18+
19+
content = response.content
20+
21+
soup = BeautifulSoup(response.content)
22+
containers = soup.find_all("div", class_="lister-item-content")
23+
for result in containers:
24+
name1 = result.h3.a.text
25+
name = result.h3.a.text.lower()
26+
# print(name)
27+
year = result.h3.find(
28+
"span", class_="lister-item-year text-muted unbold"
29+
).text.lower()
30+
31+
if title in name and release in year:
32+
rating = result.find("div",class_="inline-block ratings-imdb-rating")["data-value"]
33+
print(f"Rating of {name1}:", rating)
34+
file1.write(name1)
35+
file1.write(',')
36+
file1.write(rating)
37+
file1.write('\n')
38+
genre = result.p.find("span", class_="genre")
39+
for x in genre:
40+
print(x)
41+
42+
except Exception:
43+
print("Try again with valid combination of tile and release year")
44+
45+
file1.close()

0 commit comments

Comments
 (0)