使用TMDB API获取的Python对象
我该如何使用从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。
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
对象里的结果。你也可以通过 movielist = list(movies)
来获取一个 Movie
对象的列表。