2015-10-14 102 views
2

我只是得到了python的萦绕,我想开始使用它的API等。我安装了Tweepy,获取了我的Twitter密钥,然后在他们的网站上进行了安装过程。这是将细钱柜,好了,这里不明白Tweepy错误

import tweepy 

auth = tweepy.OAuthHandler('qwertyuiop', 'asdfghjkl') 
auth.set_access_token('qazsecftgbhujmkolp', 'plokmjuhbgtfcdeszaq') 

api = tweepy.API(auth) 

user = tweepy.api.get_user('twitter') 

print user.screen_name 

(与我的真实密钥,当然)

此方法返回的错误

Traceback (most recent call last): 
    File "tweeter.py", line 8, in <module> 
    user = tweepy.api.get_user('twitter') 
    File "/home/jeremiah/.local/lib/python2.7/site-packages/tweepy/binder.py", line 243, in _call 
    return method.execute() 
    File "/home/jeremiah/.local/lib/python2.7/site-packages/tweepy/binder.py", line 189, in execute 
    raise TweepError('Failed to send request: %s' % e) 
tweepy.error.TweepError: Failed to send request: local variable 'auth' referenced before assignment 

对此我只是不明白。定义auth是我做的第一件事。此外,这是从网站的介绍课程。

看来这一切都很好,直到user = tweepy.api.get_user('twitter')。如果我把它+下面的所有东西都拿出来,没关系,或者如果我用它来代替

public_tweets = api.home_timeline() 
for tweet in public_tweets: 
    print tweet.text 

这很好。

那是什么? Tweepy是否扭曲,是我的电脑扭曲,还是我只是失去了明显的东西?

感谢所有帮助

+0

如果您取出'print user.screen_name',你确定没问题吗?看起来错误在'user = ...'行 – Claudiu

+1

然而,它在'binder.py'中讨论'auth'对象的错误,不是你的。可能是一个问题,[见这里](https://github.com/tweepy/tweepy/blob/master/tweepy/binder.py#L173)。 – cdonts

+0

@Claudiu是的,我搞砸了。编辑。 –

回答

1

您应该能够使用tweepy.API classget_user方法(您已经分配给您的变量名api)。

尝试:

user = api.get_user(id='__your__username__') # returns the user specified by id 

或者:

user = api.me() # returns YOUR authenticated user object 

,你将需要在括号注明您的用户名,或使用me()方法(而不是字面'twitter'(返回的原因用户对象@twitter,因为您要求它获得,即用户!)是因为get_user method

返回有关指定用户的信息。

参数:
id - 指定用户的ID或屏幕名称。

user_id - 指定用户的ID。当有效的用户ID也是一个有效的屏幕名称时有助于消除歧义。

screen_name - 指定用户的屏幕名称。当有效的屏幕名称也是用户ID时,有助于消除歧义。

关于你的代码,注意,这之间的差异,这引发了一个异常:

user = tweepy.api.get_user('twitter'). 

而这,不引发异常

public_tweets = api.home_timeline() '## NOT tweepy.api.home_timeline() 

在后者(这不没有错误),您正确使用您的api变量来调用home_timeline方法。

+0

那是行不通的。但在它下面它描述了'api.me'方法。这应该会返回我的信息,所以我不必经历所有这些环节,但是当我尝试'user = api.me'然后我打印用户时它仍然不起作用。 –

+0

你在做'user = api.me'或'user = api.me()'吗? –

+0

哇。总是那么简单。猜猜我太累了,无法正确思考。无论如何,谢谢你回答我的愚蠢问题。 –