我正在尝试从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)将代码复制到新脚本,如下所示:
的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
我可以看到它做这样的事情的唯一原因是它拒绝生成的访问令牌。我不应该更新访问令牌吗?
答案 0 :(得分:6)
如果你愿意尝试另一个图书馆,你可以给rauth一个镜头。已经a Twitter example但是如果你感觉很懒,只想要一个有效的例子,这就是我修改那个演示脚本的方法:
from rauth import OAuth1Service
# Get a real consumer key & secret from https://dev.twitter.com/apps/new
twitter = OAuth1Service(
name='twitter',
consumer_key='J8MoJG4bQ9gcmGh8H7XhMg',
consumer_secret='7WAscbSy65GmiVOvMU5EBYn5z80fhQkcFWSLMJJu4',
request_token_url='https://api.twitter.com/oauth/request_token',
access_token_url='https://api.twitter.com/oauth/access_token',
authorize_url='https://api.twitter.com/oauth/authorize',
base_url='https://api.twitter.com/1/')
request_token, request_token_secret = twitter.get_request_token()
authorize_url = twitter.get_authorize_url(request_token)
print 'Visit this URL in your browser: ' + authorize_url
pin = raw_input('Enter PIN from browser: ')
session = twitter.get_auth_session(request_token,
request_token_secret,
method='POST',
data={'oauth_verifier': pin})
params = {'screen_name': 'github', # User to pull Tweets from
'include_rts': 1, # Include retweets
'count': 10} # 10 tweets
r = session.get('statuses/user_timeline.json', params=params)
for i, tweet in enumerate(r.json(), 1):
handle = tweet['user']['screen_name'].encode('utf-8')
text = tweet['text'].encode('utf-8')
print '{0}. @{1} - {2}'.format(i, handle, text)
您可以按原样运行,但请务必更新凭据!这些仅用于演示目的。
完全披露,我是rauth的维护者。
答案 1 :(得分:5)
你得到401回复,这意味着“未经授权”。 (see HTTP status codes)
您的代码看起来不错。使用api.user_timeline(screen_name="some_screen_name")
在旧的例子中为我工作,我躺在那里。
我猜您需要授权该应用,或者您的OAuth设置存在问题。
也许您已经找到了,但这是我开始的简短代码示例:https://github.com/nloadholtes/tweepy/blob/nloadholtes-examples/examples/oauth.py