Этот скрипт распечатает все роли для актера:
import requests
from bs4 import BeautifulSoup
url = 'https://www.imdb.com/name/nm4043618/'
soup = BeautifulSoup(requests.get(url).content, 'html.parser')
seen = set()
for row in soup.select('#filmo-head-actor + div .filmo-row > br'):
role = row.find_next(text=True).strip()
if not role in seen:
seen.add(role)
print(role)
Печать:
Peter Parker / Spider-Man
Nathan Drake
Todd Hewitt
Nico Walker
Arvin Russell
Ian Lightfoot (voice)
Jip (voice)
Walter (voice)
Samuel Insull
Brother Diarmuid - The Novice
Jack Fawcett
Bradley Baker
Thomas Nickerson
Tom
Gregory Cromwell
Former Billy (Encore) (uncredited)
Isaac
Eddie (voice)
Boy
Lucas
Shô (UK version, voice)
РЕДАКТИРОВАТЬ: Чтобы получить роли в DataFrame, вы можете сделать это:
import requests
import pandas as pd
from bs4 import BeautifulSoup
url = "https://www.imdb.com/name/nm4043618/"
soup = BeautifulSoup(requests.get(url).content, "html.parser")
seen = set()
all_data = []
for row in soup.select("#filmo-head-actor + div .filmo-row > br"):
role = row.find_next(text=True).strip()
if not role in seen:
seen.add(role)
all_data.append(role)
df = pd.DataFrame(all_data, columns=["Role"])
print(df)
Печать:
Role
0 Peter Parker / Spider-Man
1 Nathan Drake
2 Todd Hewitt
3 Nico Walker
4 Arvin Russell
5 Ian Lightfoot (voice)
6 Jip (voice)
7 Walter (voice)
8 Samuel Insull
9 Brother Diarmuid - The Novice
10 Jack Fawcett
11 Bradley Baker
12 Thomas Nickerson
13 Tom
14 Gregory Cromwell
15 Former Billy (Encore) (uncredited)
16 Isaac
17 Eddie (voice)
18 Boy
19 Lucas
20 Shô (UK version, voice)