2017-06-15 47 views
0

所以我即將開始在這一次。它昨天讓我煩擾了好幾個小時,所以我想我最好問你一點點幫助。Python/JSON:Loop themoviedb api圖像調用

我有一個形象的ID

tt1211837 
tt0151804 
tt2543164 
tt2911666 
tt2140479 

列表的txt文件,我似乎無法得到它通過使用該ID的線路環路

tmdb_posters('ID') 

這裏是沒有循環的代碼(這對它自己很有用)。

import os 
import requests 
import random 


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 = '*****' 

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

def get_poster_urls(imdbid): 

    config = _get_json(CONFIG_PATTERN.format(key=KEY)) 
    base_url = config['images']['base_url'] 
    sizes = config['images']['poster_sizes'] 

    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) 
    print random.choice(urls) 

if __name__=="__main__": 
     tmdb_posters('tt1211837') # <--- ID 

但是,當我試圖把這個成環的任何形式,我得到一個400客戶端錯誤和另一個告訴我,JSON鍵「海報」是壞的。

非常感謝,如果你可以提前幫忙。我想保持頭髮。

UPDATE

import urllib 
import urllib2 
from bs4 import BeautifulSoup 
import sys 
import time 
import requests 
import random 

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 = '*****' 


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


def get_poster_urls(imdbid): 

    config = _get_json(CONFIG_PATTERN.format(key=KEY)) 
    base_url = config['images']['base_url'] 
    sizes = config['images']['poster_sizes'] 

    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) 
    print random.choice(urls) 




file = open("data/titles3.txt", "r") 

splits = file.read().splitlines() 
for line in splits: 
    if __name__ == "__main__": 
     file = open("data/posters.txt", "a") 
     woop = tmdb_posters(line) 
     file.write(str(woop)) 
     file.close() 

更新2

import urllib 
import urllib2 
from bs4 import BeautifulSoup 
import sys 
import time 
import requests 
import random 

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 = '*****' 


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


def get_poster_urls(imdbid): 

    config = _get_json(CONFIG_PATTERN.format(key=KEY)) 
    base_url = config['images']['base_url'] 
    sizes = config['images']['poster_sizes'] 

    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) 
    print random.choice(urls) 


with open('data/titles3.txt', 'r') as ids: 
     ids = ids.read().split('\n') 
     for poster in ids: 
      if __name__ == "__main__": 
       file = open("data/posters.txt", "a") 
       file.write(str(tmdb_posters(poster)) + "\n") 
       file.close() 

SUSSED IT

import urllib 
import urllib2 
from bs4 import BeautifulSoup 
import sys 
import time 
import requests 
import random 

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 = '*****' 


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


def get_poster_urls(imdbid): 

    config = _get_json(CONFIG_PATTERN.format(key=KEY)) 
    base_url = config['images']['base_url'] 
    sizes = config['images']['poster_sizes'] 

    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) 
    woop = random.choice(urls) 
    file = open("data/posters.txt", "a") 
    file.write(str(woop) + "\n") 
    file.close() 


with open('data/titles3.txt', 'r') as ids: 
     ids = ids.read().split('\n') 
     for poster in ids: 
      if __name__ == "__main__": 
       tmdb_posters(poster) 

回答

0

我不能尚未置評,但它似乎是工作在一個蠻好的循環。 你可以發佈代碼,你如何閱讀ID

+0

昨天我嘗試了幾種方法,但這裏是我挖出的一個方法。不太確定,但我知道它是正確的。 文件=打開( 「數據/ titles3.txt」, 「R」) 用於文件行: 如果__name __ == 「__ main__」: 打印tmdb_posters(線) – impshum

+0

會有一個換行符('\ n')添加在每行的末尾,所以你需要刪除它 – Stack

+0

這似乎合乎邏輯。 – impshum