使用 Tweepy 将推文插入 MySQL 数据库

Inserting tweets into MySQL DB using Tweepy

我正在尝试使用以下 Python 代码将解析后的推文插入 MySQL 数据库:

    #-*- coding: utf-8 -*-

    __author__ = 'sagars'

    import pymysql
    import tweepy
    import time
    import json
    from tweepy import Stream
    from tweepy import OAuthHandler
    from tweepy.streaming import StreamListener



class listener(StreamListener):

 def on_data(self, data):
        all_data = json.loads(data)
        tweet = all_data["text"]
        username = all_data["user"]["screen_name"]

        c.execute("INSERT INTO tweets (tweet_time, username, tweet) VALUES (%s,%s,%s)"
                  (time.time(), username, tweet))
        print (username, tweet)
        return True

def on_error(self, status):
    print (status)

auth = OAuthHandler(ckey, csecret)
auth.set_access_token(atoken, asecret)
twitterStream = Stream(auth, listener())
twitterStream.filter(track = ["LeBron James"])

但是我运行出现了以下错误:

Traceback (most recent call last):
  File "C:/Users/sagars/PycharmProjects/YouTube NLP Lessons/Twitter Stream to DB.py", line 45, in <module>
    twitterStream.filter(track = ["LeBron James"])
  File "C:\Python34\lib\site-packages\tweepy\streaming.py", line 428, in filter
    self._start(async)
  File "C:\Python34\lib\site-packages\tweepy\streaming.py", line 346, in _start
    self._run()
  File "C:\Python34\lib\site-packages\tweepy\streaming.py", line 286, in _run
    raise exception
  File "C:\Python34\lib\site-packages\tweepy\streaming.py", line 255, in _run
    self._read_loop(resp)
  File "C:\Python34\lib\site-packages\tweepy\streaming.py", line 309, in _read_loop
    self._data(next_status_obj)
  File "C:\Python34\lib\site-packages\tweepy\streaming.py", line 289, in _data
    if self.listener.on_data(data) is False:
  File "C:/Users/sagars/PycharmProjects/YouTube NLP Lessons/Twitter Stream to DB.py", line 35, in on_data
    (time.time(), username, tweet))
TypeError: 'str' object is not callable

如何调整代码避免错误?推文是否应该以不同的方式从 JSON 对象中解析出来?

你忘记了前面的逗号 ([=15th=](), username....等等

澄清一下

 c.execute("INSERT INTO tweets (tweet_time,     username, tweet) VALUES (%s,%s,%s)" ,
              (time.time(), username, tweet))