Skip to content Skip to sidebar Skip to footer

Tweepy: Attributeerror: 'tuple' Object Has No Attribute 'followed_by'

I am trying to make a 'simple' script that will unfollow users whom I am following that are not following me back using the Tweepy module for Python 3.5. import sys, time, tweepy

Solution 1:

First of all, if you read the twitter doc carefully, the raw API returns {target: .., source: ..}, not {followed_by: .., ..}.

Secondly, you are working with Tweepy, which is a wrapper of the raw API. According to the Tweepy doc, it returns a friendship object (http://tweepy.readthedocs.org/en/v3.2.0/api.html#API.show_friendship). However, it does not explain how we can use this object. Going to its source, https://github.com/tweepy/tweepy/blob/master/tweepy/models.py#L237, it returns a tuple source, target. Both source and target has a followed_by attribute. I'm not sure which one you are looking for, but here's how you would access them:

source, target = relationship
print(source.followed_by)
print(target.followed_by)

Solution 2:

Direct answer to your doubt & alternative approach to your goal (below)

For the newer version of Tweepy (3.5.0), the show_friendship() returns a tuple of two elements, each one belonging to each user. For example:

result= api.show_friendship(A, B)
result

Returns the tuple

(Friendship(blocked_by=False, muting=False,..., screen_name = A, ...), Friendship(blocked_by=False, muting=False,..., screen_name = B, ...)

Then if you want to access the attribute followed_by just do the following:

result[0].followed_by

And you will get the attribute you are asking for.

Alternative approach to your goal:

If you need that just to check who is following you and who is not, a simple way to discover it is by getting the set difference between the people you follow and the people that follows you. In order to do so, you could apply the code I provide below:

import tweepy

Consumer Key = 'XXXXXXXXXXXX'
ConsumerSecret = 'XXXXXXXXXXXX'
AccessToken = 'XXXXXXXXXXXX'
AccessTokenSecret = 'XXXXXXXXXXXX'

#Keys for the program
auth = tweepy.OAuthHandler(ConsumerKey, ConsumerSecret)
auth.set_access_token(AccessToken, AccessTokenSecret)

#Inialize the API:
api = tweepy.API(auth)

#Get your followers and friends:
friendNames, followNames = [], []
for friend, follower in zip(tweepy.Cursor(api.friends).items(),
                            tweepy.Cursor(api.followers).items()):
    friendNames.append(friend.name)
    followNames.append(follower.name)

#Create sets to see who is not following you:
friendset = set(friendNames)
followset = set(followNames)
not_fback = friendset.difference(followset)

And the variable not_fback will be a set with all the users that are not following you back.

Solution 3:

The @Jzbach's code has an error, the for cicles must to be separated because is improbable that you have the same number of followers and friends.

With this patch the script extract the right subsets

#Get your followers and friends:
friendNames, followNames = [], []

for friend in tweepy.Cursor(api.friends).items():
	friendNames.append(friend.screen_name)

for follower in tweepy.Cursor(api.followers).items():
    followNames.append(follower.screen_name)

Solution 4:

You can change code like below.

Instead of this

print(relationship.followed_by)

use this line of code:

print(relationship[0].followed_by)

Post a Comment for "Tweepy: Attributeerror: 'tuple' Object Has No Attribute 'followed_by'"