Open rsurati opened 1 year ago
The error you're encountering is because tmdbv3api returns a custom object, and you're trying to access its attributes using getitem syntax
corrected get_genre
function:-
from tmdbv3api import Movie
import requests
import numpy as np
tmdb_movie = Movie()
def get_genre(x):
genres = []
result = tmdb_movie.search(x)
if len(result) != 0:
movie_id = str(result[0].id)
response = requests.get('https://api.themoviedb.org/3/movie/{}?api_key={}'.format(movie_id, tmdb.api_key))
data_json = response.json()
if 'genres' in data_json and data_json['genres']:
genre_str = " "
for genre in data_json['genres']:
genres.append(genre['name'])
return genre_str.join(genres)
else:
return np.NaN
else:
return np.NaN
and update the data frame:-
df['genres'] = df['Title'].map(lambda x: get_genre(str(x)))
Hope this helps Thanks
TypeError Traceback (most recent call last)
Cell In[188], line 1, in (x) ----> 1 df['genres'] = df['Title'].map(lambda x: get_genre(str(x)))
Cell In[186], line 7, in get_genre(x) 5 result = tmdb_movie.search(x) 6 if ( len(result) != 0 ): ----> 7 movie_id = str(result[0]['id']) 8 response = requests.get('https://api.themoviedb.org/3/movie/{}?api_key={}'.format(movie_id,tmdb.) 9 data_json = response.json()
File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\tmdbv3api\as_obj.py:47, in AsObj.getitem(self, key) 45 return self._obj_list[key] 46 else: ---> 47 return getattr(self, key)
TypeError: attribute name must be string, not 'int'
Here is the genre method:
from tmdbv3api import Movie tmdb_movie = Movie() def get_genre(x): genres = [] result = tmdb_movie.search(x) if ( len(result) != 0 ): print(type(result[0])) movie_id = str(result[0].id) response = requests.get('https://api.themoviedb.org/3/movie/{}?api_key={}'.format(movie_id,tmdb.api_key)) data_json = response.json() if data_json['genres']: genre_str = " " for i in range(0,len(data_json['genres'])): genres.append(data_json['genres'][i]['name']) return genre_str.join(genres) else: np.NaN else: np.NaN