Insert tweets into MySQL DB using Tweepy

I am trying to use the following Python code to insert processed tweets into a MySQL database:

    #-*- 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"])

But I ran into the following error:

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

How can I adjust the code to avoid an error? Should tweets be parsed from a JSON object differently?

+4
source share
1 answer

You forgot the comma before (time.time (), usernam .... etc.

To clarify this, there will be

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

All Articles