字符串索引必须是整数,而不是str tweepy

时间:2014-11-27 18:41:33

标签: python json stream tweepy tweets

您好我想从twitter流中仅保存文本和坐标到json, 当on_status我使用self.output.write(status)时它工作正常,但是当尝试编写特定功能时,“字符串索引必须是整数,而不是str”错误。有任何帮助的想法吗?谢谢!

代码基于本教程http://badhessian.org/2012/10/collecting-real-time-twitter-data-with-the-streaming-api/

class SListener(StreamListener):

def __init__(self, api = None, fprefix = 'streamer'):
    self.api = api or API()
    self.counter = 0
    self.fprefix = fprefix
    self.output  = open(fprefix + '.' 
                        + time.strftime('%Y%m%d-%H%M%S') + '.json', 'w')
    self.delout  = open('delete.txt', 'a')

def on_data(self, data):

    if  'in_reply_to_status' in data:
        self.on_status(data)
    elif 'delete' in data:
        delete = json.loads(data)['delete']['status']
        if self.on_delete(delete['id'], delete['user_id']) is False:
            return False
    elif 'limit' in data:
        if self.on_limit(json.loads(data)['limit']['track']) is False:
            return False
    elif 'warning' in data:
        warning = json.loads(data)['warnings']
        print warning['message']
        return false

def on_status(self, status):

    self.output.write("\t".join([status['text'], status['text']['coordinates']]) + "\n")        
    self.counter += 1

    if self.counter >= 1000:
        self.output.close()
        self.output = open(self.fprefix + '.' 
                           + time.strftime('%Y%m%d-%H%M%S') + '.json', 'w')
        self.counter = 0

    return

def on_delete(self, status_id, user_id):
    self.delout.write( str(status_id) + "\n")
    return

def on_limit(self, track):
    sys.stderr.write(track + "\n")
    return

def on_error(self, status_code):
    sys.stderr.write('Error: ' + str(status_code) + "\n")
    return False

def on_timeout(self):
    sys.stderr.write("Timeout, sleeping for 60 seconds...\n")
    time.sleep(60)
    return 

0 个答案:

没有答案