python - tweepy 流到 sqlite 数据库 - 语法无效

标签 python twitter sqlite tweepy

下面的代码正在为一个变量流式传输 Twitter 公共(public)时间线,该变量将任何推文输出到控制台。我想将相同的变量(status.text、status.author.screen_name、status.created_at、status.source)保存到 sqlite 数据库中。当我的脚本看到一条推文但没有任何内容写入 sqlite 数据库时,我收到语法错误。

错误:

$ python stream-v5.py @lunchboxhq
Filtering the public timeline for "@lunchboxhq"RT @LunchboxHQ: test 2   LunchboxHQ  2012-02-29 18:03:42 Echofon
Encountered Exception: near "?": syntax error

代码:

import sys
import tweepy
import webbrowser
import sqlite3 as lite

# Query terms

Q = sys.argv[1:]

sqlite3file='/var/www/twitter.lbox.com/html/stream5_log.sqlite'

CONSUMER_KEY = ''
CONSUMER_SECRET = ''
ACCESS_TOKEN = ''
ACCESS_TOKEN_SECRET = ''

auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)

con = lite.connect(sqlite3file)
cur = con.cursor()
cur.execute("CREATE TABLE TWEETS(txt text, author text, created int, source text)")

class CustomStreamListener(tweepy.StreamListener):

    def on_status(self, status):

        try:
            print "%s\t%s\t%s\t%s" % (status.text, 
                                      status.author.screen_name, 
                                      status.created_at, 
                                      status.source,)

            cur.executemany("INSERT INTO TWEETS(?, ?, ?)", (status.text, 
                                                            status.author.screen_name, 
                                                            status.created_at, 
                                                            status.source))

        except Exception, e:
            print >> sys.stderr, 'Encountered Exception:', e
            pass

    def on_error(self, status_code):
        print >> sys.stderr, 'Encountered error with status code:', status_code
        return True # Don't kill the stream

    def on_timeout(self):
        print >> sys.stderr, 'Timeout...'
        return True # Don't kill the stream

streaming_api = tweepy.streaming.Stream(auth, CustomStreamListener(), timeout=60)

print >> sys.stderr, 'Filtering the public timeline for "%s"' % (' '.join(sys.argv[1:]),)

streaming_api.filter(follow=None, track=Q)

最佳答案

您在以下代码的最后一行(您发布的第 34-37 行)缺少右括号:

            cur.executemany("INSERT INTO TWEETS(?, ?, ?)", (status.text, 
                                                        status.author.screen_name, 
                                                        status.created_at, 
                                                        status.source)

只需在您的元组参数后添加一个括号即可立即关闭方法调用。

关于python - tweepy 流到 sqlite 数据库 - 语法无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9434205/

相关文章:

database - SQLite:更新相同数据时会写入磁盘吗?

python - key 错误 : Creating a dataframe column using square root of the sum of square of two column values

python - 列出 pandas dataframe 列中的所有单词

python - 使用 BeautifulSoup4 和 Python 3 解析 html 表

javascript - ajax 和变量范围

c# - 在 LINQ group by procedure 之后访问 List<T> 中保存的对象属性

android - 为什么使用 Android Room 时没有创建数据库文件?

Python如何找到列表中的最大元素

api - 如何使用 Twitter 搜索 API 返回与我的搜索查询匹配的所有推文,仅在最近五秒内发布?

android - ContentValues 条目正在转换为 NULL