2015-10-14 59 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

哇。總是那麼簡單。猜猜我太累了,無法正確思考。無論如何,謝謝你回答我的愚蠢問題。 –