Python - 使用json和twitter的“字符串索引必须是整数”错误

时间:2018-05-29 18:43:56

标签: python json twitter

所以我正在进行一项任务,并在尝试解析我返回的json文件时不断收到此错误。我将2个不同的部分组合在一起,只做了很小的改动。我一直收到错误“字符串索引必须是整数”这是代码和我正在使用的2张幻灯片。任何帮助将不胜感激!

Slide 1

Slide 2

from twython import Twython

ConsumerKey = "..."
ConsumerSecret = "..."
AccessToken = "..."
AccessTokenSecret = "..."

twitter = Twython(ConsumerKey,
    ConsumerSecret,
    AccessToken,
    AccessTokenSecret)

result = twitter.search(q="drexel")

for tweet in result:
    print(" User: {0} \n Created: {1} \n Text: {2} \n "
        .format(tweet ['user'] ['name'],
            tweet['created_at'],
            tweet ['text']))

1 个答案:

答案 0 :(得分:0)

json是一个多维数组。我意识到凯文建议使用打印(推文)后返回状态& search_metadata。这是更新后的代码!

from twython import Twython

ConsumerKey = "...e"
ConsumerSecret = "..."
AccessToken = "..."
AccessTokenSecret = "..."

twitter = Twython(ConsumerKey,
	ConsumerSecret,
	AccessToken,
	AccessTokenSecret)
	
result = twitter.search(q="drexel")

result1 = result['statuses']

for tweet in result1:
	print(" User: {0} \n Created: {1} \n Text: {2} \n "
		.format(tweet ['user'] ['name'],
			tweet['created_at'],
			tweet ['text']))