【问题标题】:Use python object retrieved with TMDB API使用通过 TMDB API 检索的 python 对象
【发布时间】:2013-02-11 21:43:24
【问题描述】:

如何使用通过 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

【问题讨论】:

  • 你能把config['urls']['movie.search']返回的json贴出来吗?
  • 是的 @dm03514 是这样的:config['urls']['movie.search'] = "https://api.themoviedb.org/3/search/movie?query=%%s&api_key=%(apikey)s&page=%%s" % (config)

标签: python api object printing themoviedb-api


【解决方案1】:

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

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 对象中的结果。您还可以使用movielist = list(movies) 获取Movie 对象的列表。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-09
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多