如何解决此IndexError超出范围?

时间:2016-08-30 20:30:03

标签: python indexing

为什么我的列表索引超出范围错误?我该如何解决?

我尝试在逻辑中的 else 之后进入,但结果是一样的。

if int_list_repost is '':
            int_list_repost = [0]

但结果是相同的

  

Traceback(最近一次调用最后一次):文件   “C:/Users/ayevtushenko/PycharmProjects/Tuts/Post_Engagement_pin_logic.py”   第99行       get_post('https://twitter.com/ClinRev')文件“C:/Users/ayevtushenko/PycharmProjects/Tuts/Post_Engagement_pin_logic.py”,   第95行,在get_post中       'retweeted_posts':len(int_list_repost [1:]),'pinned_retweets':int_list_repost [0],IndexError:列表索引超出范围

import requests
import re
from bs4 import BeautifulSoup

def get_post(url):

    source_code = requests.get(url)
    plain_text = source_code.text
    my_soup = BeautifulSoup(plain_text)

    mylist = []
    int_list = []
    mylist_repost = []
    int_list_repost = []
    pinned = ""

    #   GETS "PINNED" TEXT IF PINNED
    for content in my_soup.findAll('span', {'class': 'js-pinned-text'}):
        pinned = str(content.string)

    #   PUTS FAVORITE METRICS INTO LIST
    for content in my_soup.findAll('div', {'class': 'ProfileTweet-action ProfileTweet-action--favorite js-toggleState'}):
        fetch = content.contents[1]
        for tag in fetch.findAll('span', {'class': 'ProfileTweet-actionCountForPresentation'}):
            mylist.append(tag.string)
            if str(tag.string).isdigit():
                int_list.append(int(tag.string))

    #   PUTS RE-POST METRICS INTO LIST
    for content in my_soup.findAll('div', {'class': 'ProfileTweet-action ProfileTweet-action--retweet js-toggleState js-toggleRt'}):
        fetch = content.contents[1]
        for tag in fetch.findAll('span', {'class': 'ProfileTweet-actionCountForPresentation'}):
            mylist_repost.append(tag.string)
            if str(tag.string).isdigit():
                int_list_repost.append(int(tag.string))

    like_page_utilization = str((len(int_list)/len(mylist))*100)+'%'
    repost_page_utilization = str((len(int_list_repost)/len(mylist_repost))*100)+'%'

    #   TOTAL ENGAGEMENT METRICS
    largest_list = [len(int_list), len(int_list_repost)]
    largest_list_max = max(largest_list)
    total_engagements_overall = sum(int_list)+sum(int_list_repost)

    overall_engagement_utilization = str((largest_list_max/len(mylist))*100)+'%'

    if pinned != 'Pinned Tweet':
        return {'liked_posts': len(int_list), 'total_likes': sum(int_list),
                'pinned_likes': 0, 'pinned': 'F', 'like_page_utilization': like_page_utilization,
                'repost_page_utilization': repost_page_utilization,
                'overall_engagement_utilization': overall_engagement_utilization,
                'retweeted_posts': len(int_list_repost), 'pinned_retweets': 0,
                'total_retweets': sum(int_list_repost),
                'total_engagements_overall': total_engagements_overall}
    else:
        return {'liked_posts': len(int_list[1:]), 'total_likes': sum(int_list[1:]),
                'pinned_likes': int_list[0], 'pinned': 'T', 'like_page_utilization': like_page_utilization,
                'repost_page_utilization': repost_page_utilization,
                'overall_engagement_utilization': overall_engagement_utilization,
                'retweeted_posts': len(int_list_repost[1:]), 'pinned_retweets': int_list_repost[0],
                'total_retweets': sum(int_list_repost[1:]),
                'total_engagements_overall': total_engagements_overall}

2 个答案:

答案 0 :(得分:1)

您已初始化一个数组,这是一个对象。当您询问变量是否为null时,该值始终为false。它不是空的,它是一个对象。

如果要测试您的数组是否为空,请使用以下代码:

is len(int_list_repost) == 0:
    print("int_list_repost is empty")

但那不是非常pythonic。空数组的测试应该是

is not int_list_repost:
    print("int_list_repost is empty")

您收到的错误消息是因为如果您有一个空数组,int_list_repost[0]将会出错。

答案 1 :(得分:0)

该消息非常简单poll() ...如果您检查脚本输出,它会显示一个空数组:'pinned_retweets': int_list_repost[0] Index Out Of Range,因此您需要在最后一步之前检查数组长度,例如这样:

raw int only list []

测试小提琴:http://www.codeskulptor.org/#user41_1EOs9Swi3K_0.py