1

以下のコードは、ツイートをコンソールに出力する変数のTwitterパブリックタイムラインをストリーミングしています。同じ変数(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)
4

4 に答える 4

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))
于 2012-02-24T17:20:11.640 に答える
2

次のコードの最後の行(投稿したものの34〜37行目)に閉じ括弧がありません。

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

タプルパラメータの直後にかっこを追加して、メソッド呼び出しを閉じます。

于 2012-02-27T05:42:33.227 に答える
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'])

私は間違いなくウィザードではなく、commit()がすべてのエントリにどのような影響を与えるかわかりません...パフォーマンスはひどいものだと思いますが、クエリ内の1つの用語で機能します。

あなたのコードを投稿してくれてありがとう、私はついにストリーミングをする方法を学びました。

于 2013-02-01T09:00:15.560 に答える
0

私はtweepyにまったく慣れていません。しかし、これらは私のために働いた変更です。INSERTINTOTWEETSの後にVALUESを追加する必要があります。また、変更をコミットすることを忘れないでください。これは私が参照したリンクです:関連記事

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

     con.commit()
于 2014-01-27T19:38:51.407 に答える