-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
92 lines (73 loc) · 2.94 KB
/
main.py
File metadata and controls
92 lines (73 loc) · 2.94 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import requests
from bs4 import BeautifulSoup
import json
def format_imdb_url(url):
"""Ensure full IMDb URL"""
trimmed_url = url.split('?')[0]
if not trimmed_url.startswith("https://www.imdb.com/"):
trimmed_url = "https://www.imdb.com" + trimmed_url
return trimmed_url
def extract_next_page(url):
"""Fetch movie description from individual IMDb page"""
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)'
}
try:
response = requests.get(url, headers=headers, timeout=10)
soup = BeautifulSoup(response.content, 'html.parser')
# Try plot-l, then plot-xl (IMDb changes frequently)
description_tag = soup.find('span', {'data-testid': 'plot-l'})
if not description_tag:
description_tag = soup.find('span', {'data-testid': 'plot-xl'})
return description_tag.text.strip() if description_tag else 'No description available.'
except Exception as e:
return f"Failed to fetch description: {str(e)}"
def extract_info(query):
"""Search IMDb and extract top movie results"""
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)'
}
# Construct search URL
response = requests.get(query, headers=headers)
soup = BeautifulSoup(response.content, 'html.parser')
# IMDb results are in <li> with class 'ipc-metadata-list-summary-item'
results = soup.find_all('li', class_='ipc-metadata-list-summary-item')
extracted_data = []
for result in results[:5]: # Limit to top 5
# Title
title_tag = result.find('a', class_='ipc-metadata-list-summary-item__t')
if not title_tag:
continue
title = title_tag.text.strip()
partial_url = title_tag['href']
full_url = format_imdb_url(partial_url)
# Year
year_tag = result.find('span', class_='ipc-metadata-list-summary-item__li')
year = year_tag.text.strip() if year_tag else "Unknown"
# Actors
actors = "Unknown"
actor_ul = result.find_all(
'ul',
class_='ipc-inline-list ipc-inline-list--show-dividers ipc-inline-list--no-wrap ipc-inline-list--inline ipc-metadata-list-summary-item__stl base'
)
if actor_ul:
actor_lis = actor_ul[0].find_all('li')
actors = ", ".join(li.text.strip() for li in actor_lis)
# Description from detail page
description = extract_next_page(full_url)
# Append all
extracted_data.append({
'title': title,
'url': full_url,
'year': year,
'actors': actors,
'description': description
})
return extracted_data
# Uncomment to test as a script
# if __name__ == "__main__":
# query = input("Enter movie name: ")
# data = extract_info(query)
# print(json.dumps(data, indent=4))
# with open("imdb_results.json", "w") as f:
# json.dump(data, f, indent=4)