我试图从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