2011-04-29 66 views
0

如果我使用0而不是x(在while循環中)該程序的作品,我從flickr從某人的最愛列表中獲得第一張照片,但如果我想要更多超過或比第一照片等我得到這個錯誤爲什麼我不斷收到IndexError:列表索引超出範圍錯誤

import flickrlib 
import webbrowser 
FLICKR_API_KEY = "f86203e922041a6a999fd4a59f19b1e6" 
FLICKR_API_SSECRET = "b286bcaddafb00c8" 

flickruser = raw_input("Who are you interested in? ") 
amount = int(raw_input("How many pictures would you like to see? ")) 
total = amount 
counter = 0 
imagepile = ""   
x= 0 
client = flickrlib.FlickrAgent(FLICKR_API_KEY, FLICKR_API_SSECRET) 
person = client.flickr.people.findByUsername(username= flickruser) 
userid = person[u'id'] 
photos = client.flickr.favorites.getPublicList(user_id= userid, per_page=1) 

while counter < total: 
    farm = photos[u'photo'] [x] [u'farm'] 
    server = photos[u'photo'] [x] [u'server'] 
    photo_id = photos[u'photo'] [x] [u'id'] 
    secret = photos[u'photo'] [x] [u'secret'] 
    imgsrc = "<img src='http://farm" + farm + ".static.flickr.com/" + server + "/" + photo_id + "_" + secret +".jpg' /><br />" 
    imagepile = imagepile + imgsrc 
    counter= counter+1 
    x=x+1 
htmlopen = "<html><head><title>"+ flickruser + "</title></head><body><h1>" + flickruser + "'s Public List. ""</h1>" 

simple_page = htmlopen + imagepile + "</body></html>" 

xml_file = open("webpage.html", "w") 
xml_file.write(simple_page) 
xml_file.close() 

webbrowser.open("webpage.html") 
+3

請發佈您收到的'IndexError'的完整輸出。 – jathanism 2011-04-29 13:56:09

回答

5

我不知道Flickr的API,但它看起來像你只要求1張照片getPublicList。無論哪種方式,您都不檢查以確保total小於從getPublicList返回的圖片數量。

+1

發佈後+1我看到你先到那裏:) – MByD 2011-04-29 13:59:54

3

因爲你使用per_page=1這裏:

photos = client.flickr.favorites.getPublicList(user_id= userid, per_page=1) 

,你只能得到一張照片。

相關問題