Reputation: 702
Why am I getting this list index out of range error? And how can I fix it?
I tried entering into after the else in the logic, but the result was the same.
if int_list_repost is '':
int_list_repost = [0]
but the result was the same
Traceback (most recent call last): File "C:/Users/ayevtushenko/PycharmProjects/Tuts/Post_Engagement_pin_logic.py", line 99, in get_post('https://twitter.com/ClinRev') File "C:/Users/ayevtushenko/PycharmProjects/Tuts/Post_Engagement_pin_logic.py", line 95, in get_post 'retweeted_posts': len(int_list_repost[1:]), 'pinned_retweets': int_list_repost[0], IndexError: list index out of range
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}
Upvotes: 0
Views: 344
Reputation: 123
You've initialized an array, which is an object. When you ask if the variable is null, that will always be false. It's not null, it's an object.
If you want to test to see if your array is empty use this code:
is len(int_list_repost) == 0:
print("int_list_repost is empty")
But that's not very pythonic. The test for empty arrays should be
is not int_list_repost:
print("int_list_repost is empty")
The error message you are getting is because int_list_repost[0]
will error out if you have an empty array.
Upvotes: 1
Reputation: 12305
The message is pretty straightforward 'pinned_retweets': int_list_repost[0] Index Out Of Range
... And if you check your script output, it shows an empty array: raw int only list []
, so you need to check the array length before the last step, like this:
if len(int_list_repost) == 0:
int_list_repost.append(0)
print 'pinned_retweets', int_list_repost[0]
Upvotes: 0