Tweepy: AttributeError: 'tuple' object has no attribute 'followed_by'

血红的双手。 提交于 2019-12-13 01:41:17

问题


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

auth = tweepy.OAuthHandler('Consumer Key', 'Consumer Secret')
auth.set_access_token('Access Token', 'Access Token Secret')

api = tweepy.API(auth)

for page in tweepy.Cursor(api.friends, screen_name='My Twitter Handle').pages():
    for friend in page:
        relationship = api.show_friendship(source_screen_name='My Twitter Handle', target_screen_name=friend.screen_name)
        print(relationship.followed_by)
        time.sleep(13)
print('\nDone.')
sys.exit()

Currently, the aim of the above code is to simply print out the users who are not following me back. When the code is executed, Python throws this at me:

AttributeError: 'tuple' object has no attribute 'followed_by'

I know this not to be true, as Twitter's documentation mentions it here.

However, I am no expert, hence why I am asking this question here. Any idea what I am doing wrong?


回答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)



回答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.




回答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)



回答4:


You can change code like below.

Instead of this

print(relationship.followed_by)

use this line of code:

print(relationship[0].followed_by)


来源:https://stackoverflow.com/questions/34663831/tweepy-attributeerror-tuple-object-has-no-attribute-followed-by

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!