[英]I'm getting an List index out of range error for an index that exist
Im using feed parser to obtain rss objects. 我使用feed解析器来获取rss对象。 When I run
我跑的时候
live_leak.links
I get 我明白了
[{'type': 'text/html', 'rel': 'alternate', 'href':
'http://www.liveleak.com/view?i=abf_1476121939'},
{'type': 'application/x-shockwave-flash', 'rel': 'enclosure', 'href':
'http://www.liveleak.com/e/abf_1476121939'}]
But when i try this 但是,当我尝试这个
live_leak.links[1]
I get list index out of range, mind you this was working earlier then all of a sudden this didn't work. 我得到列表索引超出范围,请注意,这是在早些时候工作然后突然间这不起作用。 I had this in my code and it took me hours to find because I didn't realize it was this that wasn't working.
我在我的代码中有这个,我花了几个小时才找到,因为我没有意识到这是不起作用的。 If no one knows I'll do a string replace as a hack but I rather do what was already working.
如果没有人知道我会做一个字符串替换作为黑客,但我宁愿做已经工作的。
this also works 这也有效
live_leak[0]
it returns 它返回
[{'type': 'text/html', 'rel': 'alternate', 'href':
'http://www.liveleak.com/view?i=abf_1476121939'}]
which is weird because the other one won't work 这很奇怪,因为另一个不会起作用
def pan_task():
url = 'http://www.liveleak.com/rss?featured=1'
name = 'live leak'
live_leaks = [i for i in feedparser.parse(url).entries]
the_count = len(live_leaks)
ky = feedparser.parse(url).keys()
oky = [i.keys() for i in feedparser.parse(url).entries][:12] # shows what I can pull
try:
live_entries = [{
'html': live_leak.links,
'href': live_leak.links[0]['href'],
'src': live_leak.media_thumbnail[0]['url'],
'text': live_leak.title,
'comments': live_leak.description,
'url': live_leak.links[0]['href'],
'embed': live_leak.links[1]['href'],
'text': live_leak.title,
'comments': live_leak.description,
'name': name,
'url': live_leak.link, # this is the link to the source
'author': None,
'video': False
} for live_leak in live_leaks]
except IndexError:
print('error check logs')
live_entries = []
# for count, elem in enumerate(live_entries):
# the_html = requests.get(live_entries[count]['url']) # a specific text
return print(live_entries[0])
You can index live_leak , but live_leak.links appears to be some other type of construct that returns the elements of live_leak . 您可以为live_leak编制索引,但live_leak.links似乎是返回live_leak元素的其他类型的构造。 Try live_leak[1] , perhaps?
试试live_leak [1]吧?
One is looking for links under live_leak the other is just looking at live_leak itself. 一个是在live_leak下查找链接,另一个是查看live_leak本身。
for example: live_leak[1] 例如:live_leak [1]
should return: [{'type': 'application/x-shockwave-flash', 'rel': 'enclosure', 'href': ' http://www.liveleak.com/e/abf_1476121939 '}] 应该返回:[{'type':'application / x-shockwave-flash','rel':'enclosure','href':' http : //www.liveleak.com/e/abf_1476121939 '}]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.