コマンドラインで入力したユーザー名にTweepyを使用して、Twitterからデータを取得しようとしています。ステータスとユーザーに関するかなりのデータを抽出したいので、次のことを考え出しました。
必要なすべてのモジュールをインポートして、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の制限まで)で反復処理したいと思います。でもまず最初に。これまでのところ、2つの問題がありますが、リツイートに関して次のエラーメッセージが表示されます。
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データベースに転送し(画面に出力するのではなく、それを実行します)、データを操作して処理できるようにする方法を知っています。問題が発生していることがわかります。 。誰かアイデアがありますか、それとも私が検討すべき別の方法がありますか?
どんな助けでも本当にありがたいです。乾杯
編集:
今朝の@EricOlsonの提案に続きます。私は次のことをしました。
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
このようなことをしていることがわかる唯一の理由は、生成されたアクセストークンを拒否していることです。アクセストークンを更新する必要はありませんか?