【问题标题】:tweepy stream to sqlite database - invalid synatxtweepy 流到 sqlite 数据库 - 语法无效
【发布时间】:2012-03-15 02:34:13
【问题描述】:

下面的代码正在流式传输推特公共时间线,以获取将任何推文输出到控制台的变量。我想将相同的变量(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)

【问题讨论】:

  • 您能否提供问题中的语法错误,以便我们了解更多上下文?

标签: python twitter sqlite tweepy


【解决方案1】:

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

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

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

【讨论】:

  • 没问题。有助于有第二双眼睛来捕捉错别字。您可能需要考虑在合理对比的配色方案中使用语法突出显示,以便您的编辑器可以帮助您避免或发现这些错误。
  • 当我的脚本看到一条推文并且没有任何内容写入 sqlite 数据库时,我仍然收到语法错误。
【解决方案2】:
import sqlite3 as lite
con = lite.connect('test.db')
cur = con.cursor()   

cur.execute("CREATE TABLE TWEETS(txt text, author text, created int, source text)")

然后:

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

【讨论】:

  • 它抛出一个错误。我认为我做对了。 ' 文件 "stream-v5.py",第 39 行,异常除外,e: ^ SyntaxError: invalid syntax ' pastebin.com/mLqwUa16
  • 当我的脚本看到一条推文并且没有任何内容写入 sqlite 数据库时,我仍然收到语法错误。
  • 抱歉,sql调用的参数太少,试试:cur.executemany("INSERT INTO TWEETS(?, ?, ?, ?)", (status.text, status.author. screen_name, status.created_at, status.source))
【解决方案3】:

完全披露:对这些东西还是陌生的。但是,我通过将您的代码更改为:

cur.execute("INSERT INTO TWEETS VALUES(?,?,?,?)", (status.text, status.author.screen_name, status.created_at, status.source))
con.commit()

在我看来,您一次只能以一种状态阅读。 executemany 方法适用于您拥有多个状态的情况。例如:

(['sometext', 'bob','2013-02-01','Twitter for Android'], ['someothertext', 'helga', '2013-01-31', 'MacSomething'])

我绝对不是一个向导,我不确定 commit() 对每个条目有什么样的影响......我猜性能很糟糕,但它适用于查询中的单个术语。

感谢您发布您的代码,我终于学会了如何进行流式传输。

【讨论】:

    【解决方案4】:

    我对 tweepy 很陌生。但这些是对我有用的修改。您需要在 INSERT INTO TWEETS 之后添加 VALUES 。另外,不要忘记提交更改。这是我提到的链接:related post

         cur.execute("INSERT INTO TWEETS VALUES(?, ?, ?, ?)", (status.text, 
                                                            status.author.screen_name, 
                                                            status.created_at, 
                                                            status.source))
    
         con.commit()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多