0

如何使用通过 TMDB API 检索到的一些数据?

这是功能:

class Movies(Core):
    def __init__(self, title="", limit=False):
        self.limit = limit
        self.update_configuration()
        title = self.escape(title)
        self.movies = self.getJSON(config['urls']['movie.search'] % (title,str(1)))
        pages = self.movies["total_pages"]
        if not self.limit:
            if int(pages) > 1:                  #
                for i in range(2,int(pages)+1): #  Thanks @tBuLi
                    self.movies["results"].extend(self.getJSON(config['urls']['movie.search'] % (title,str(i)))["results"])

    def __iter__(self):
        for i in self.movies["results"]:
            yield Movie(i["id"])

    def get_total_results(self):
        if self.limit:
            return len(self.movies["results"])
        return self.movies["total_results"]

    def iter_results(self):
        for i in self.movies["results"]:
            yield i

和电话:

def search_tmdb(title):
  tmdb.configure(TMDB_KEY)
  movie = tmdb.Movies(title,limit=True)

问题是,我如何查看和使用对象影片的结果?

我很抱歉这个可能很愚蠢的问题,但我现在正在接近 python

4

1 回答 1

0

看起来您可以执行以下操作:

movies = tmdb.Movies(title,limit=True)
#if you want to deal with Movie objects
for movieresult in movies:
    #do something with the Movie result (here I'm just printing it)
    print movieresult

#if you want to deal with raw result (not wrapped with a Movie object)
for result in movies.iter_results():
    #do something with the raw result
    print result

tmdb.Movies(title, limit=True)创建一个Movies对象。由于__iter__方法已定义,您可以使用该方法查看对象for movie in moviesobject中的结果Movies。您还可以Movie使用movielist = list(movies).

于 2013-02-11T23:15:35.150 回答