Tweepy: AttributeError: 'tuple' 对象没有属性 'followed_by' [英] Tweepy: AttributeError: 'tuple' object has no attribute 'followed_by'

查看:35
本文介绍了Tweepy: AttributeError: 'tuple' 对象没有属性 'followed_by'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个简单"的脚本,该脚本将取消关注我关注的用户,而这些用户没有使用 Python 3.5 的 Tweepy 模块关注我.

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()

目前,上述代码的目的是简单地打印出没有关注我的用户.当代码执行时,Python 向我抛出这个:

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'

我知道这不是真的,因为 Twitter 的文档提到了它这里.

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?

推荐答案

首先,如果你仔细阅读 Twitter 文档,原始 API 返回 {target: .., source: ..},而不是 {后跟:.., ..}.

First of all, if you read the twitter doc carefully, the raw API returns {target: .., source: ..}, not {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.sourcetarget 都有一个 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)

这篇关于Tweepy: AttributeError: 'tuple' 对象没有属性 'followed_by'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆