我正在运行以下代码以及其他一些代码,但我正面临着与此部分有关的一些问题。
idx_to_movie = {}
with open('u.item', encoding = "ISO-8859-1") as f:
for line in f.readlines():
info = line.split('|')
idx_to_movie[int(info[0])-1] = info[4]
def top_k_movies(similarity, mapper, movie_idx, k=6):
return [mapper[x] for x in np.argsort(similarity[movie_idx,:])[:-k-1:-1]]
idx = 0 # Toy Story
movies = top_k_movies(item_similarity, idx_to_movie, idx)
posters = tuple(Image(url=get_poster(movie, base_url)) for movie in movies)
KeyError Traceback(最近一次调用最后一次)
<ipython-input-78-17abeb8d67ed> in
get_poster(imdb_url, base_url)
尝试: file_path = json.loads(response.text)['posters'] [0] ['file_path'] 不同的是:
KeyError:'海报'
在处理上述异常期间,发生了另一个异常:
IndexError Traceback(最近一次调用最后一次)
<ipython-input-85-a456f8efd408> in <module>()
idx = 0 # Toy Story
movies = top_k_movies(item_similarity, idx_to_movie, idx)
posters = tuple(Image(url=get_poster(movie, base_url)) for movie in movies)
<ipython-input-85-a456f8efd408> in <genexpr>(.0)
idx = 0 # Toy Story
movies = top_k_movies(item_similarity, idx_to_movie, idx)
posters = tuple(Image(url=get_poster(movie, base_url)) for movie in movies)
in
get_poster(imdb_url, base_url)
payload['query'] = movie_title
response = requests.get('http://api.themoviedb.org/3/search/movie', params=payload, headers=headers)
movie_id = json.loads(response.text)['results'][0]['id']
payload.pop('query', None)
movie_url = 'http://api.themoviedb.org/3/movie/{:}/images'.format(movie_id)
IndexError:列表索引超出范围