在Python27中无法解码JSON对象

时间:2015-10-10 20:54:23

标签: python json

这是我的Python代码:

import os
import requests

CONFIG_PATTERN = 'http://api.themoviedb.org/3/configuration?api_key={key}'
IMG_PATTERN = 'http://api.themoviedb.org/3/movie/{imdbid}/images?api_key={key}' 
KEY = '<apikey>'

def _get_json(url):
    r = requests.get(url)
    return r.json()

def _download_images(urls, path='.'):
    """download all images in list 'urls' to 'path' """

    for nr, url in enumerate(urls):
        r = requests.get(url)
        filetype = r.headers['content-type'].split('/')[-1]
        filename = 'poster_{0}.{1}'.format(nr+1,filetype)
        filepath = os.path.join(path, filename)
        with open(filepath,'wb') as w:
            w.write(r.content)

def get_poster_urls(imdbid):
    """ return image urls of posters for IMDB id

        returns all poster images from 'themoviedb.org'. Uses the
        maximum available size. 

        Args:
            imdbid (str): IMDB id of the movie

        Returns:
            list: list of urls to the images
    """
    config = _get_json(CONFIG_PATTERN.format(key=KEY))
    base_url = config['images']['base_url']
    sizes = config['images']['poster_sizes']

    """
        'sizes' should be sorted in ascending order, so
            max_size = sizes[-1]
        should get the largest size as well.        
    """
    def size_str_to_int(x):
        return float("inf") if x == 'original' else int(x[1:])
    max_size = max(sizes, key=size_str_to_int)

    posters = _get_json(IMG_PATTERN.format(key=KEY,imdbid=imdbid))['posters']
    poster_urls = []
    for poster in posters:
        rel_path = poster['file_path']
        url = "{0}{1}{2}".format(base_url, max_size, rel_path)
        poster_urls.append(url) 

    return poster_urls

def tmdb_posters(imdbid, count=None, outpath='.'):    
    urls = get_poster_urls(imdbid)
    if count is not None:
        urls = urls[:count]
    _download_images(urls, outpath)

if __name__=="__main__":
    tmdb_posters('tt1375666')

这是我的问题:

Traceback (most recent call last):
  File "tmdb.py", line 64, in <module>
    tmdb_posters('tt1375666')
  File "tmdb.py", line 58, in tmdb_posters
    urls = get_poster_urls(imdbid)
  File "tmdb.py", line 35, in get_poster_urls
    config = _get_json(CONFIG_PATTERN.format(key=KEY))
  File "tmdb.py", line 10, in _get_json
    return r.json()
  File "C:\Python27\lib\site-packages\requests-2.8.0-py2.7.egg\requests\models.py", line 797, in json
    self.content.decode(encoding), **kwargs
  File "C:\Python27\lib\json\__init__.py", line 338, in loads
    return _default_decoder.decode(s)
  File "C:\Python27\lib\json\decoder.py", line 366, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Python27\lib\json\decoder.py", line 384, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

我只是想使用themoviedb API下载'tt1375666'电影的海报。我怎样才能解决我的问题?我是Python的业余爱好者,我使用Python27。谢谢你的帮助!

0 个答案:

没有答案