2012-02-24 69 views
1

下面的代码是流的twitter公共时间线为变量输出任何推文到控制台。我想保存相同的变量(status.text,status.author.screen_name,status.created_at,status.source)到sqlite数据库中。当我的脚本看到一条推文而没有写入sqlite数据库时,我收到了语法错误。tweepy流到sqlite数据库 - 无效synatx

错误:

$ 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) 
+0

你能在你的问题提供了语法错误,所以我们可以有更多的背景? – jmlane 2012-02-29 16:42:02

回答

2

你缺少下列的最后一行右括号代码(第34-37行来自你发布):

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

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

+0

谢谢修复它。 – 2012-02-27 17:27:54

+0

没问题。有助于让第二双眼睛接触错别字。您可能需要考虑在合理对比的颜色方案中使用语法高亮显示,以便您的编辑器可以帮助您避免或发现这些错误。 – jmlane 2012-02-28 06:25:41

+0

当我的脚本看到推文而没有写入sqlite数据库时,我仍然收到语法错误。 – 2012-02-29 16:14:43

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)) 
+0

这是抛出一个错误。我认为我做对了。 '文件 “stream-v5.py” 39行 除例外,E: ^ 语法错误:无效的语法 ' http://pastebin.com/mLqwUa16 – 2012-02-24 21:32:49

+0

我仍然得到一个语法错误,当我脚本看到一条推文,没有任何东西写入sqlite数据库。 – 2012-02-29 16:15:10

+0

抱歉,它在sql调用中的参数太少,请尝试:cur.executemany(“INSERT INTO TWEETS(?,?,?,?)”,(status.text, status.author.screen_name, status.created_at , status.source)) – cbz 2012-03-06 12:03:33

0

完全披露:这个东西还是新的。但是,我通过将代码更改为:

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'])

我绝对不是一个向导,我不知道该提交什么样的影响()对每个条目......我猜的表现是可怕的,但它的工作原理查询中的单个词语。

感谢您发布您的代码,我终于学会了如何做流媒体。

0

我是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()