I've started using snscrape to get twitter data for my project. I'm outputting the data in a list format and using pandas to properly visualize it. since the engagement data is not constant and consistenly updates. I'm trying to use, a for loop and an if statement to compare and update it if needed but when I try to use index to access the specific properties in the list like:
like, retweet and reply count
It gives me an index error.
Script
#get data
import snscrape.modules.twitter as sntwitter
import pandas as pd
tweets_list1 = []
for i, tweet in enumerate(sntwitter.TwitterSearchScraper('from:TheHoopCentral').get_items()):
if i > 100:
break
tweets_list1.append([tweet.content, tweet.id,tweet.likeCount, tweet.retweetCount, tweet.replyCount, tweet.user.username])
print(tweets_list1)
tweets_df1 = pd.DataFrame(tweets_list1, columns=['text', 'Tweet id','LikeCount', 'RetweetCount', 'ReplyCount', 'Username'])
tweets_df1.head()
#updating engagement
tweets_list1 = []
for i, tweet in enumerate(sntwitter.TwitterSearchScraper('TheHoopCentral').get_items()):
if tweet.likeCount > tweets_list1[2]:
tweets_list1.insert(2, tweet.likeCount)
elif i > 100:
break
Error
IndexError Traceback (most recent call last)
<ipython-input-26-c679f6d5eecc> in <module>
3 tweets_list1 = []
4 for i, tweet in enumerate(sntwitter.TwitterSearchScraper('TheHoopCentral').get_items()):
----> 5 if tweet.likeCount > tweets_list1[2]:
6 tweets_list1.insert(2, tweet.likeCount)
7 elif i > 100:
IndexError: list index out of range
CodePudding user response:
In my opinion the main issue is with the reassignment of tweets_list1 in the updating engagement section.
It seems like you are assigning an empty list to variable tweets_list1, before performing the loop logic
tweets_list1 = [] # <-- Here
for i, tweet in enumerate(sntwitter.TwitterSearchScraper('TheHoopCentral').get_items()):
if tweet.likeCount > tweets_list1[2]:
tweets_list1.insert(2, tweet.likeCount)
elif i > 100:
break
Then, during the first loop, you will try to access the 3rd element of an empty list, therefore causing the error.
tweets_list1 = []
for i, tweet in enumerate(sntwitter.TwitterSearchScraper('TheHoopCentral').get_items()):
if tweet.likeCount > tweets_list1[2]: # <-- Here
tweets_list1.insert(2, tweet.likeCount)
elif i > 100:
break