Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
656 views
in Technique[技术] by (71.8m points)

python - tweepy stream to sqlite database - invalid synatx

The code below is streaming the twitter public timeline for a variable which output any tweets to the console. I'd like the save the same variables (status.text, status.author.screen_name, status.created_at, status.source) into an sqlite database. I'm getting an syntax error when my script sees a tweet and nothing is written to the sqlite database.

the error:

$ 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

the code:

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%s%s%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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are missing a closing parenthesis on the last line of the following code (lines 34–37 from what you posted):

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

Just add a parenthesis to close the method call immediately after your tuple parameter.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...