[英]throwing an error -String indices must be integers
#I did the same as you suggested the output at python shell is errors #我做的和您建议的一样,python shell的输出是错误
from rauth import OAuth1Service 从rauth导入OAuth1Service
twitter = OAuth1Service(
name='twitter',
consumer_key='2Xs6nzIL8r5yx4DQCxgg',
consumer_secret='XXXXXXXX',
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': '@nytimes', # 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):
print tweet
#handle = tweet['user']['screen_name']
#text = tweet['text']
#print('{0}. @{1} - {2}'.format(i, handle, text))
Either your tweet
is a str
or your tweet
is a dict
and your tweet['user']
is a str
. 您的tweet
是str
还是您的tweet
是dict
而您的tweet['user']
是str
。 You seem to expect that tweet['user']
is a dict and try to index in that with tweet['user']['screen_name']
. 您似乎希望tweet['user']
是一个字典,并尝试使用tweet['user']['screen_name']
进行索引。 This means that you are trying to index a str
with another str
which is not possible (as it makes no sense). 这意味着您正在尝试用一个不可能的另一个str
索引一个str
(因为这没有意义)。 The resulting error message states exactly that: TypeError: string indices must be integers
. 产生的错误消息确切说明: TypeError: string indices must be integers
。
I suppose you give out your tweet
and your tweet['user']
(using print tweet
and print tweet['user']
, but do not give out both in the same print statement, otherwise an error in the second expression will prevent the output at all) to figure out what the data structures are here. 我想您给出tweet
和tweet['user']
(使用print tweet
和print tweet['user']
,但不要在同一print语句中都给出它们,否则第二个表达式中的错误将阻止输出)以了解此处的数据结构。 Then you probably know how to go on by yourself, or you post here again what you found out so we can go on helping. 然后,您可能知道自己如何进行操作,或者您在这里再次发布了发现的内容,以便我们继续提供帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.