【发布时间】:2018-09-20 10:14:20
【问题描述】:
在__init__ 方法中,当我尝试访问get_movie_detail 时,它会显示对象没有属性的错误。当我尝试从列表中获取数据时会发生这种情况。当我尝试在get_movie_detail 中返回单个变量时,例如。 return title,它成功返回了标题,但如果我尝试处理列表,则会出现以下错误:
堆栈跟踪:
674
Harry Potter and the Goblet of Fire
7.6
Traceback (most recent call last):
File "C:\Users\HOME\Desktop\movie trailer\entertainment.py", line 4, in <module>
movie_name_1=media.Data('Harry potter and goblet of fire')
File "C:\Users\HOME\Desktop\movie trailer\media.py", line 73, in __init__
self.title = detail_new.detail_list[0]
AttributeError: 'list' object has no attribute 'detail_list'
下面是我的代码:
""" stores requested movie data"""
import webbrowser
import urllib.request
import json
import tmdbsimple as tmdb
tmdb.API_KEY = ''
class Data():
"""takes argument movie_name,searches the data from tmdb database
tmdbSimple is wrapper&classes of tmdb
movie name keyword should be precise so that api can fetch data
accurately.Incomplete keyword could lead to unwanted results.
Attributes:
Search():search method in tmdbsimple.
movie:movie method takes query and return results
search_result:getd the result of searched movies includes
data regarding movies in list format.
movie_id:stores the unique movie ID for a particular movie
title:stores title of movie.
imdb_rating:rating of movie.
movie_overview:short description of movie.
release_date:date on which movie released.
data_yt:searches the movie keyword on youtube api for trailer.
youtube_key:gets key from the search result in json format
trailer.youtube_url:complete youtube link to the trailer
poster_image_url:link to the poster of movie."""
def get_movie_detail(self,movie_name):
print("in am in get_movie_detail")
#search movie id related to query from tmdb api
search = tmdb.Search()
response_old = search.movie(query=movie_name)
search_result = search.results
s_find = search_result[0]#gets the first id of the search result
movie_id = s_find['id']
print(movie_id)
#uses movie id to get the details of movie
movie = tmdb.Movies(movie_id)
response = movie.info()#stores the movie information in dict format
title = response['title']#if you try to print title in the definition it will print correctly
print(title)
vote = response['vote_average']
print(vote)
discript = response['overview']
date = response['release_date']
poster_id = response['poster_path']
detail_list = [title,vote,discript,date,poster_id]
return detail_list
"""" issue is that if i try calling from __init__ its not returning any attributes of get_movie_detail,Is there scope issue?"""
def get_trailer_link(self,movie_name):
print("I am in get_trailer")
query_new = urllib.parse.quote_plus(movie_name+'trailer')
data_yt = urllib.request.urlopen("https://www.googleapis.com/youtube/v3/search?part=snippet&maxResults=5&q="+query_new+"&type=video&key=")
trailer_data = data_yt.read()
youtube_json = json.loads(trailer_data)
youtube_key = str(youtube_json['items'][0]['id']['videoId'])
def __init__(self,movie_name):
"""stores the atrributes of instances"""
detail_new = self.get_movie_detail(movie_name)
self.title = detail_new.detail_list[0] #***while trying to get data from method it shows error:'NoneType' object has no attribute 'title'***
print(self.title)
self.imdb_rating = detail_new.vote.detail_list[1]
print(self.imdb_rating)
self.movie_overview = detail_new.detail_list[2]
print(self.movie_overview)
self.release_date = detail_new.detail_list[3]
print(self.release_date)
#uses youtube api to get the url from the search result
#gets movie image from the database tmdbsimple api
self.trailer_youtube_url = "https://www.youtube.com/watch?v="+str(self.get_trailer_link.youtube_key)
self.poster_image_url = "http://image.tmdb.org/t/p/w185"+str(poster_id)
def trailer(self):
"""opens youtube url"""
webbrowser.open(self.trailer_youtube_url)
【问题讨论】:
-
请发布实际的堆栈跟踪......它告诉我们错误的确切位置。此外,python 的一个良好调试实践是在测试脚本中消除大部分杂乱以缩小问题范围。把这个简化的例子贴在这里是件好事。
-
“不返回任何属性”是什么意思?它返回什么?如果你
print(detail_new)会发生什么?如果你在方法中print(detail_list)怎么办? -
另外,由于该方法似乎根本没有使用
self:如果您将其拉出到独立函数然后从顶级代码调用它会发生什么,例如@987654330 @? -
当我在方法中打印 detail_list 时,它会正确打印数据。但是当我尝试从 init 函数调用时,它会返回。 . 'list' 对象没有属性 'detail_list'
-
我尝试将该函数 get_movie_detail 放在类之外并尝试调用它,但它仍然返回相同的错误
标签: python list function class init