我正在尝试使用Tweepy从Twitter检索数据,该数据是通过命令行输入的用户名。我想要提取有关状态和用户的大量数据,因此想出了以下内容:
请注意,我已经成功导入了所有必需的模块,并且具有oauth + keys(只是没有在此处包含),文件名也是正确的,只是已更改:
# 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)将代码复制到一个新脚本中,如下所示:
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
我唯一能想到的原因是它拒绝了生成的访问令牌。我不需要更新访问令牌,对吧?