我正在尝试制作一个“简单”脚本,该脚本将取消关注我所关注的用户,而这些用户没有使用 Python 3.5 的 Tweepy 模块来关注我。
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()
目前,上述代码的目的是简单地打印出没有关注我的用户。当代码执行时,Python 向我抛出这个:
AttributeError: 'tuple' object has no attribute 'followed_by'
我知道这不是真的,正如 Twitter 的文档提到的 here .
但是,我不是专家,因此我在这里问这个问题。知道我做错了什么吗?
最佳答案
首先,如果您仔细阅读 Twitter 文档,原始 API 将返回 {target: .., source: ..},而不是 {followed_by: .., ..}
。
其次,您正在使用 Tweepy,它是原始 API 的包装器。根据 Tweepy 文档,它返回一个 friendship
对象 ( http://tweepy.readthedocs.org/en/v3.2.0/api.html#API.show_friendship )。但是,它没有解释我们如何使用这个对象。追根溯源,https://github.com/tweepy/tweepy/blob/master/tweepy/models.py#L237 ,它返回一个元组源,目标
。
source
和 target
都有一个 followed_by
属性。我不确定您要寻找哪一个,但访问它们的方法如下:
source, target = relationship
print(source.followed_by)
print(target.followed_by)
关于python - Tweepy:AttributeError: 'tuple'对象没有属性 'followed_by',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34663831/