Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
807 views
in Technique[技术] by (71.8m points)

python - AttributeError: using Tweepy and Twitter API

I am trying to extract information from Twitter's API using Tweepy. 90% of the code works fine, but when I try to get the followers_count from each tweet, I run into an error.

The code I am using is:

# Running only on handle returns a dataframe 
tweets = api.user_timeline(user_id=user_id, count=number_tweets, tweet_mode="extended")
print("Number of tweets extracted: {}.
".format(len(tweets)))
data = pd.DataFrame(data=[tweet.full_text for tweet in tweets], columns= ["Text"])
data["Tweet length"] = np.array([len(tweet.full_text) for tweet in tweets])
data["Tweet ID"] = np.array([tweet.id for tweet in tweets])
data["Tweet Date"] = np.array([tweet.created_at for tweet in tweets])
data["Tweet source"] = np.array([tweet.source for tweet in tweets])
data["Tweet likes"] = np.array([tweet.favorite_count for tweet in tweets])
data["Tweet retweets"] = np.array([tweet.retweet_count for tweet in tweets])
# Line to be added here #
data = data.sort_values(by="Tweet Date", ascending = False)

When I got to add in the following line:

data["User followers"] = np.array([tweet.followers_count for tweet in tweets])

The error message is:

AttributeError: 'Status' object has no attribute 'followers_count'

question from:https://stackoverflow.com/questions/65845316/attributeerror-using-tweepy-and-twitter-api

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your code in the first snippet refers to the Tweet Object, i.e. they all refer to an instance of a Tweet that you will be able to get the values from using the Attributes. You can see this by the name of the attributes.

On the second snippet

data["User followers"] = np.array([tweet.followers_count for tweet in tweets])

This is not a Tweet object but instead a User Object. Therefore the dictionary that you refer to (tweets) will not have attributes with the name of followers_count. You can see from the documentation here that Tweet object does not contain any followers_count attribute.

In order to solve this, you need to make a request to get the User Object. You can see the documentation for that here - User object


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...