如何使用 Python 检索给定用户的所有推文和属性?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15628535/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I retrieve all Tweets and attributes for a given user using Python?
提问by chowden
I am attempting to retrieve data from Twitter, using Tweepy for a username typed at the command line. I'm wanting to extract quite a bit of data about the status and user,so have come up with the following:
我正在尝试从 Twitter 检索数据,使用 Tweepy 作为在命令行输入的用户名。我想提取很多关于状态和用户的数据,所以想出了以下内容:
Note that I am importing all the required modules ok and have oauth + keys (just not included it here) and filename is correct, just been changed:
请注意,我正在导入所有必需的模块,并且具有 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)
I would like this eventually to iterate through all of a user's tweets (up to the 3200 limit). First things first though. So far though I have two problems, I get the following error message regarding retweets:
我希望最终能够遍历用户的所有推文(最多 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
Passing the username as a variable seems to be a problem also:
将用户名作为变量传递似乎也是一个问题:
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
I've isolated both these errors, i.e. they aren't working together.
我已经隔离了这两个错误,即它们不能一起工作。
Forgive my ignorance, I am not too hot with Twitter APIs but am learning pretty rapidly. Tweepy documentation really does suck and I've done loads of reading round on the net, just can't seem to get this fixed. If I can get this sorted, i'll be posting up some documentation.
原谅我的无知,我对 Twitter API 不太感兴趣,但我学得很快。Tweepy 文档确实很糟糕,我在网上做了大量的阅读,但似乎无法解决这个问题。如果我能解决这个问题,我会发布一些文档。
I know how to transfer the data into an MySQL db once extracted (it will do that, rather than print to screen) and manipulate it so that I can do stuff with it, it is just getting it out that I am having the problems with. Does anyone have any ideas or is there another method I should be considering?
我知道如何在提取后将数据传输到 MySQL 数据库中(它会这样做,而不是打印到屏幕上)并对其进行操作,以便我可以用它做一些事情,它只是解决了我遇到的问题. 有没有人有任何想法,或者我应该考虑另一种方法吗?
Any help really appreciated. Cheers
任何帮助真的很感激。干杯
EDIT:
编辑:
Following on from @Eric Olson's suggestion this morning; I did the following.
遵循今天早上@Eric Olson 的建议;我做了以下事情。
1) Created a completely brand new set of Oauth credentials to test. 2) Copied code across to a new script as follows:
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")
The first time i run the script, it works fine and updates my status and returns the API name as follows:
我第一次运行脚本时,它工作正常并更新我的状态并返回 API 名称,如下所示:
>>>
API NAME IS: Chris Howden
Then from that point on I get this:
然后从那时起我得到了这个:
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
The only reason I can see for it doing something like this is that it is rejecting the generated access token. I shouldn't need to renew the access token should I?
我可以看到它做这样的事情的唯一原因是它拒绝生成的访问令牌。我应该不需要更新访问令牌吗?
采纳答案by maxcountryman
If you're open to trying another library, you could give rautha shot. There's already a Twitter examplebut if you're feeling lazy and just want a working example, here's how I'd modify that demo script:
如果你愿意尝试另一个图书馆,你可以试一试劳斯。已经有一个 Twitter 示例,但如果您觉得懒惰并且只想要一个有效的示例,那么我将如何修改该演示脚本:
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)
You can run this as-is, but be sure to update the credentials! These are meant for demo purposes only.
您可以按原样运行它,但一定要更新凭据!这些仅用于演示目的。
Full disclosure, I am the maintainer of rauth.
完全披露,我是rauth的维护者。
回答by Eric Olson
You're getting 401 response, which means "Unauthorized." (see HTTP status codes)
您收到 401 响应,意思是“未经授权”。 (请参阅 HTTP 状态代码)
Your code looks good. Using api.user_timeline(screen_name="some_screen_name")works for me in the old example I have lying around.
你的代码看起来不错。api.user_timeline(screen_name="some_screen_name")在我躺着的旧示例中使用对我有用。
I'm guessing you either need to authorize the app, or there is some problem with your OAuth setup.
我猜你要么需要授权应用程序,要么你的 OAuth 设置有问题。
Maybe you found this already, but here is the short code example that I started from: https://github.com/nloadholtes/tweepy/blob/nloadholtes-examples/examples/oauth.py
也许你已经找到了,但这是我开始的简短代码示例:https: //github.com/nloadholtes/tweepy/blob/nloadholtes-examples/examples/oauth.py

