2016-03-11 1 views
0

J'essaie d'obtenir des tweets par des hashtags. Il semble les charger, mais au point d'enregistrer l'identifiant tweet, je reçois:"TypeError: String Les index doivent être des entiers" lors de l'utilisation de Twython

"TypeError: String indices must be integers". 

Je pense que le problème est quelque chose comme this.

Mon code:

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 

Le retraçage:

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 
+0

pouvez-vous s'il vous plaît copier/coller la pile complète ici? parce que TypeError peut être élevé par plusieurs lignes dans le script que vous avez posté :) – Markon

Répondre

0

La list_of_tweets variable (ou new_tweets) retournée par twitter.search est un type dictionary.

Vous n'êtes pas itérez correctement sur le dictionnaire, comme dans cet exemple:

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

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

itération correcte sur le dictionnaire utilise la méthode iteritems:

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

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

itérer sur les statuts retournés:

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