代码之家  ›  专栏  ›  技术社区  ›  user9609050

从init调用时无法获取属性值

  •  0
  • user9609050  · 技术社区  · 7 年前

    在里面 __init__ 尝试访问时的方法 get_movie_detail 它显示对象没有属性的错误。当我试图从列表中获取数据时,就会发生这种情况。当我尝试在 获取\u movie\u详细信息 例如:。 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)
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   wpercy    7 年前

    您不需要尝试访问 detail_list 你的 detail_new 就是那个列表。

    您可以通过访问 detail_new[0]

    所以你的 __init__() 功能变为:

    self.title = detail_new[0]
    self.imdb_rating = detail_new[1]
    self.movie_overview = detail_new[2]
    self.release_date = detail_new[3]