我正在尝试使用 python 和以下代码获取电影的导演姓名:
url = 'https://api.themoviedb.org/3/movie/%s/casts?api_key=############' % (tmdbid)
headers = {'Accept': 'application/json'}
request = Request(url, headers=headers)
try:
if pythonVer == 2:
output = urlopen(request, timeout=10).read()
else:
output = urlopen(request, timeout=10).read().decode('utf-8')
except Exception:
output = ''
actor = re.findall('"name":"(.*?)"', output)
actor2 = re.findall('"name":".*?"name":"(.*?)"', output)
actor3 = re.findall('"name":".*?"name":".*?"name":"(.*?)"', output)
actor4 = re.findall('"name":".*?"name":".*?"name":".*?"name":"(.*?)"', output)
actor5 = re.findall('"name":".*?"name":".*?"name":".*?"name":".*?"name":"(.*?)"', output)
actor6 = re.findall('"name":".*?"name":".*?"name":".*?"name":".*?"name":".*?"name":"(.*?)"', output)
actor7 = re.findall('"name":".*?"name":".*?"name":".*?"name":".*?"name":".*?"name":".*?"name":"(.*?)"', output)
director = re.findall('"known_for_department":"Directing","name":"(.*?)"', output)
我可以获取前 8 位演员,但无法提取导演的真实姓名,因为结果与标记为“known_for_department”的名字和导演(例如“Mo Faisal”)匹配,他不是真正的导演:
"known_for_department":"Directing",
"name":"Mo Faisal",
"original_name":"Mo Faisal",
"popularity":0.828,
"profile_path":"/a7Dmvb8L8QcGTVVmgWO1r8U28ME.jpg",
"cast_id":525,
"character":"Trooper (uncredited)",
"credit_id":"63bb9bc8ae6f093cb9de8202",
"order":61},
真正的总监就在这个阵下
"job":"Director"
保罗·范霍文在这种情况下
"known_for_department":"Directing",
"name":"Paul Verhoeven",
"original_name":"Paul Verhoeven",
"popularity":5.461,
"profile_path":"/wxkroRBtM6NKZdx9sYLFfFSGnaR.jpg",
"credit_id":"52fe4253c3a36847f801552f",
"department":"Directing",
"job":"Director"},
我如何重写此代码以仅获取标记为
的导演姓名"job":"Director"