我正在尝试从 Twitter 检索数据,使用 Tweepy 作为在命令行键入的用户名。我想提取相当多的关于状态和用户的数据,所以想出了以下内容:
请注意,我正在导入所有必需的模块,并且有 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 Olson 今天早上的建议;我做了以下。
1) 创建了一套全新的 Oauth 凭据进行测试。2)将代码复制到新脚本中,如下所示:
认证
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
我能看到它这样做的唯一原因是它拒绝生成的访问令牌。我应该不需要更新访问令牌吗?