繁体   English   中英

为什么我不断收到 IndexError: list index out of range 错误

[英]why do i keep getting a IndexError: list index out of range error

如果我使用 0 而不是 x (在 while 循环中),程序可以工作,我从 flickr 的某人最喜欢的列表中获得第一张照片,但如果我想要的不仅仅是第一张照片,我会收到此错误

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")

我不知道 Flickr 的 API,但看起来您只要求在getPublicList中提供 1 张照片。 无论哪种方式,您都不会检查以确保total小于从getPublicList返回的图片数量。

因为你在这里使用per_page=1

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

你只会得到一张照片。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM