아래 코드는 모든 트윗을 콘솔에 출력하는 변수에 대한 트위터 공개 타임 라인을 스트리밍합니다. 같은 변수 (status.text, status.author.screen_name, status.created_at, status.source)를 sqlite 데이터베이스에 저장하고 싶습니다. 내 스크립트가 트윗을보고 sqlite 데이터베이스에 아무 것도 쓰지 않으면 구문 오류가 발생합니다.tweepy stream to 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)
문맥 오류가있어서 문맥을 좀 더 가질 수 있습니까? – jmlane