Unraised exception using Tweepy and MySQL

橙三吉。 提交于 2019-12-30 11:54:27

问题


I am trying to use Tweepy to store tweets in a MySQL DB. The code works fine, with the exception of once I try to execute the SQL command to insert the data into the database. Code is as follows:

#MySQL connection attempt
try:
    cnx = mysql.connector.connect(**config)
    cursor = cnx.cursor()
except mysql.connector.Error as err:
    if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
        print("Something is wrong with your user name or password")
    elif err.errno == errorcode.ER_BAD_DB_ERROR:
        print("Database does not exist")
    else:
        print(err)

class StdOutListener(StreamListener):
    def on_data(self, data):
        tweet = json.loads(data) #loads JSON data into list for python
        #Parameters for SQL insert
        try:
            user_id = tweet['user']['id']
        except IndexError:
            print("Critical error: User ID not found")
            return
        try:
            username = tweet['user']['screen_name']
        except IndexError:
            print("Critical error: Username not found")
            return
        try:
            avatar = tweet['user']['profile_image_url']
        except IndexError:
            return
        try:
            tweet_id = tweet['id']
        except IndexError:
            print("Critical error: Tweet ID not found")
            return
        try:
            content = tweet['text']
        except IndexError:
            print("Critical error: Tweet Content not found")
            return
        try:
            #converts Twitter's date format into MySQL DATETIME
            posted = time.strftime('%Y-%m-%d %H:%M:%S', time.strptime(tweet['created_at'],'%a %b %d %H:%M:%S +0000 %Y'))
        except IndexError:
            print("Critical error: created_at not found")
            return

        #MySQL insert query
        try:
            cursor.execute("INSERT INTO tweets (user_id, username, avatar, tweet_id, content, posted) VALUES (%%s, %%s, %%s, %%s, %%s, %%s)",(user_id, username, avatar, tweet_id, content, posted))
        except RuntimeError:
            print("Runtime error")
        print("TEST")
        cnx.commit()
        return True

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




if __name__ == '__main__':
    l = StdOutListener()
    auth = OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)

    stream = Stream(auth, l)
    print("Starting Twitter Stream")
    stream.filter(track=['bob'], languages=["en"])

Then, when running I get the following error:

Traceback (most recent call last):
  File "C:/Users/Wilson/PycharmProjects/Avalon/Twitter.py", line 95, in <module>
    stream.filter(track=['*', '**', '***', '****'], languages=["en"])
  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
RuntimeError: No active exception to reraise

I have isolated it to the line that executes the MySQL query.


回答1:


By checking tweepy/streaming.py at https://github.com/tweepy/tweepy/blob/master/tweepy/streaming.py it seems there is a bug in tweepy in the way exceptions are handled, spefically

    if exception:
        # call a handler first so that the exception can be logged.
        self.listener.on_exception(exception)
        raise

This raise should be raise exception



来源:https://stackoverflow.com/questions/28869442/unraised-exception-using-tweepy-and-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!