使用Twython时,“TypeError:String Indices必须是整数”

时间:2016-03-11 12:16:22

标签: python twitter typeerror hashtag twython

我正在尝试通过主题标签获取推文。它似乎加载它们,但在保存推文ID时,我得到:

"TypeError: String indices must be integers". 

我认为问题类似于this

我的代码:

from twython import Twython, TwythonError
import time
import math
import json

def twitter_oauth_login():
    API_KEY = ""
    API_SECRET = ""
    ACCESS_TOKEN = ""
    ACCESS_TOKEN_SECRET = ""
    twitter = Twython(API_KEY,API_SECRET,ACCESS_TOKEN,ACCESS_TOKEN_SECRET)
    return(twitter)

twitter=twitter_oauth_login()

def get_minimum_id(list_of_tweets):
    ids = [] 
    idsproxy=[]
    for tweet in list_of_tweets:
        this_id = tweet['id'] #The problem appears here
        this_id = int(this_id) 
        print this_id
        ids.append(this_id) 
    if ids == idsproxy:
        minimum = 0
        return minimum
    else:
        minimum = min(ids) 
        return minimum 

def get_complete_timeline(hashtag):
     twitter = twitter_oauth_login()
     all_tweets = [] 
     max_id = None 
     while True: 
        new_tweets = twitter.search(q='#'+hashtag, count=200,    max_id=max_id)
        print u"Identified" + str(len(new_tweets)) + u" new tweet for user  " + hashtag
        all_tweets.extend(new_tweets) 

        print "Wait 10 seconds"
        time.sleep(10) 
        min_id = get_minimum_id(new_tweets) 
        max_id = min_id - 1 
        if len(new_tweets) < 100:
            break 
            print "Task complete: in total " + len(all_tweets) + " tweets were loaded."
     return all_tweets

追溯:

TypeError Traceback (most recent call last)
<ipython-input-52-708b711e136b> in <module>()
 ----> 1 get_complete_timeline("omg")

<ipython-input-51-34204cb2269e> in get_complete_timeline(hashtag)
     42         print "Wait 10 seconds"
     43         time.sleep(10)
---> 44         min_id = get_minimum_id(new_tweets) 
     45         max_id = min_id - 1 
     46         if len(new_tweets) < 100:

<ipython-input-51-34204cb2269e> in get_minimum_id(list_of_tweets)
     19         for tweet in list_of_tweets: 
     20                 print '1'
---> 21                 this_id = tweet['id'] 
     22                 print '2'
     23                 this_id = int(this_id) 

TypeError: string indices must be integers

1 个答案:

答案 0 :(得分:0)

twitter.search返回的变量list_of_tweets(或new_tweets)是dictionary类型。

您没有在字典上正确迭代,就像在此示例中一样:

d = {'a':1, 'b': 2}
for value in d:
   print value

# This would iterate over the dictionary keys
>> 'a'
>> 'b'

使用iteritems方法正确迭代字典:

d = {'a':1, 'b': 2}
for key, value in d.iteritems():
   print key, value

>> 'a', 1
>> 'b', 2

迭代返回的状态:

for tweet in list_of_tweets['statuses']:
    print tweet['id']