2012-02-24 75 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()