python - 使用 Tweepy 将推文插入 MySQL DB

标签 python mysql twitter

我正在尝试使用以下 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 对象中解析出来?

最佳答案

你在(time.time(), usernam....等之前忘记了一个逗号

澄清一下

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

关于python - 使用 Tweepy 将推文插入 MySQL DB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30927565/

相关文章:

php - 固定成本 map 库或服务?

python - 检查数据存储是否为空

python - 是否有首先合并的删除重复项选项( Pandas )

mysql - 从时间戳中选择最后连续的天数(今天除外)

mysql - 如果我在 MySQL 中删除一个设置了 foreignkey_checks=0 的表,FK 会发生什么?

swift - 无法使用 Twitter 登录

rest - 如何获得特定推文的超过 100 次转发?

javascript - 将主题标签作为参数传递给 Twitter

python - python 中的柏林噪声

python - numpy 数组中的时间值到日期时间数组