简体   繁体   English

对于存在的索引,我得到的List索引超出范围错误

[英]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 这很奇怪,因为另一个不会起作用

EDIT 编辑

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.

相关问题 我的错误列表索引超出范围 - I'm getting an error list index out of range Pygame 获取列表索引超出范围时出错,但我不知道如何修复(我是新手) - Pygame Error getting list index out of range but i dont know how to fix (I'm new) 我的代码中出现超出范围的索引错误 - I'm getting an index error out of range in my code 我在尝试打印大文件时收到 IndexError: list index out of range error in pandas - I'm getting IndexError: list index out of range error in pandas when trying print a huge file 我在 for 循环中使用 Tabulas; 收到此错误:IndexError: list index out of range - I'm using Tabulas in a for loop; getting this error: IndexError: list index out of range 当我看到列表索引非常在范围内时,是否有理由让列表索引超出范围? - Is there a reason why I'm getting a list index out of range when I'm seeing it is very much in range? 为什么我收到“列表索引超出范围”错误? - Why am I getting a 'list index out of range' error? 为什么会出现“列表索引超出范围”错误? - Why am I getting `list index out of range` error? 为什么我会收到此“列表索引超出范围”错误? - Why am I getting this “list index out of range” error? 我不断收到此错误:第 57 行列出索引超出范围 - I keep getting this error: line 57 List index out of range
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM