我試圖從Twitter中檢索數據,使用Tweepy在命令行輸入用戶名。我想提取相當多的關於狀態和用戶的數據,所以想出了以下幾點:如何檢索使用Python的給定用戶的所有Tweets和屬性?
請注意,我導入所有必需的模塊並確定oauth +鍵(只是沒有包含在這裏)和文件名是正確的,只是被改變:
# define user to get tweets for. accepts input from user
user = tweepy.api.get_user(input("Please enter the twitter username: "))
# Display basic details for twitter user name
print (" ")
print ("Basic information for", user.name)
print ("Screen Name:", user.screen_name)
print ("Name: ", user.name)
print ("Twitter Unique ID: ", user.id)
print ("Account created at: ", user.created_at)
timeline = api.user_timeline(screen_name=user, include_rts=True, count=100)
for tweet in timeline:
print ("ID:", tweet.id)
print ("User ID:", tweet.user.id)
print ("Text:", tweet.text)
print ("Created:", tweet.created_at)
print ("Geo:", tweet.geo)
print ("Contributors:", tweet.contributors)
print ("Coordinates:", tweet.coordinates)
print ("Favorited:", tweet.favorited)
print ("In reply to screen name:", tweet.in_reply_to_screen_name)
print ("In reply to status ID:", tweet.in_reply_to_status_id)
print ("In reply to status ID str:", tweet.in_reply_to_status_id_str)
print ("In reply to user ID:", tweet.in_reply_to_user_id)
print ("In reply to user ID str:", tweet.in_reply_to_user_id_str)
print ("Place:", tweet.place)
print ("Retweeted:", tweet.retweeted)
print ("Retweet count:", tweet.retweet_count)
print ("Source:", tweet.source)
print ("Truncated:", tweet.truncated)
我想這最終通過所有用戶的鳴叫的迭代(直到3200極限)。首先,但首先。到目前爲止,雖然我有兩個問題,我得到關於銳推以下錯誤消息:
Please enter the twitter username: barackobamaTraceback (most recent call last):
File " usertimeline.py", line 64, in <module>
timeline = api.user_timeline(screen_name=user, count=100, page=1)
File "C:\Python32\lib\site-packages\tweepy-1.4-py3.2.egg\tweepy\binder.py", line 153, in _call
raise TweepError(error_msg)
tweepy.error.TweepError: Twitter error response: status code = 401
Traceback (most recent call last):
File "usertimeline.py", line 42, in <module>
user = tweepy.api.get_user(input("Please enter the twitter username: "))
File "C:\Python32\lib\site-packages\tweepy-1.4-py3.2.egg\tweepy\binder.py", line 153, in _call
raise TweepError(error_msg)
tweepy.error.TweepError: Twitter error response: status code = 404
傳遞用戶名作爲變量似乎是一個問題也是:
Traceback (most recent call last):
File " usertimleline.py", line 64, in <module>
timeline = api.user_timeline(screen_name=user, count=100, page=1)
File "C:\Python32\lib\site-packages\tweepy-1.4-py3.2.egg\tweepy\binder.py", line 153, in _call
raise TweepError(error_msg)
tweepy.error.TweepError: Twitter error response: status code = 401
我已經分離出這兩種錯誤,即他們不一起工作。
原諒我的無知,我對Twitter API不太熱,但我的學習速度非常快。 Tweepy的文檔真的很爛,我已經在網上做了大量的閱讀,似乎無法得到這個固定的。如果我能得到這個排序,我會發布一些文檔。
我知道如何將數據傳輸到一個MySQL數據庫一旦提取(它會這樣做,而不是打印到屏幕),並操縱它,以便我可以做它的東西,它只是讓它出來,我是有問題的。有沒有人有任何想法,或者我應該考慮另一種方法嗎?
任何幫助真的很感激。乾杯
編輯:
從@Eric奧爾森的建議今早上;我做了以下。
1)創建一個全新的Oauth憑據集來測試。 2)複製跨越到一個新的腳本如下代碼:
的Oauth
consumer_key = "(removed)"
consumer_secret = "(removed)"
access_key="88394805-(removed)"
access_secret="(removed)"
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api=tweepy.API(auth)
# confirm account being used for OAuth
print ("API NAME IS: ", api.me().name)
api.update_status("Using Tweepy from the command line")
我第一次運行該腳本,它工作正常,並更新我的狀態和如下返回API名稱:
>>>
API NAME IS: Chris Howden
然後從這一點上我得到這個:
Traceback (most recent call last):
File "C:/Users/Chris/Dropbox/Uni_2012-3/6CC995 - Independent Studies/Scripts/get Api name and update status.py", line 19, in <module>
api.update_status("Using Tweepy frm the command line")
File "C:\Python32\lib\site-packages\tweepy-1.4-py3.2.egg\tweepy\binder.py", line 153, in _call
raise TweepError(error_msg)
tweepy.error.TweepError: Twitter error response: status code = 403
的我唯一能看到的是,它正在拒絕生成的訪問令牌。我應該不需要更新訪問令牌嗎?
王牌,謝謝你的努力。與此同時,我正在尋找另一種方法來使用tweepy模塊獲得所有我想要的內容,但這有助於更好地理解json。 – chowden 2013-04-02 10:41:30
我會發布完成時發現的內容。 – chowden 2013-04-02 10:41:56