简体   繁体   English

Python Json TypeError:字符串索引必须是整数错误

[英]Python Json TypeError: string indices must be integers error

I am studying Python while looking at the textbook.我一边看课本一边学习Python。 However, there was an error in the code I wrote after reading the book, and I found out what the problem was by searching the error source, but I don't know how to correct it.但是我看了书后写的代码出现了错误,通过搜索错误源发现了问题所在,但不知道如何改正。 TypeError: string indices must be integers TypeError:字符串索引必须是整数

import requests
from requests_oauthlib import OAuth1

consumer_key = 'check consumer_key'
consumer_secret = 'check consumer_secret'
access_token = 'check access_token'
access_token_secret = 'check access_token_secret'

oauth = OAuth1(client_key=consumer_key, client_secret=consumer_secret,
resource_owner_key=access_token, resource_owner_secret=access_token_secret)

url = 'http://api.twitter.com/1.1/statuses/user_timeline.json?screen_name={0}'.format('naver_d2')
r = requests.get(url=url,auth=oauth)

statuses = r.json()

for status in statuses:
   print(status['text'], status['created_at'])

You are trying to access a list via a string that's why you are receiving such error.You can only use status[text] if status is a dictionary您正在尝试通过字符串访问列表,这就是您收到此类错误的原因。如果 status 是字典,您只能使用status[text]
Moreover, replace your URL with https to not receive an SSL error此外,将您的 URL 替换为https不会收到 SSL 错误

URL should be "https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name={0}".format('naver_d2') URL 应该是"https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name={0}".format('naver_d2')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM