我下面的脚本(取自各种在线资源)没有写入数据库。我没有收到任何错误,如果我注释掉数据库行,那么它会毫无问题地输出到控制台。
数据库存在,它有 2 个字段,可由我编写....有什么想法吗?
下面更新了代码
#!/usr/bin/env python
import sys
import tweepy
from textwrap import TextWrapper
import sqlite3
CONSUMER_KEY = ''
CONSUMER_SECRET = ''
ACCESS_KEY = ''
ACCESS_SECRET = ''
auth1 = tweepy.auth.OAuthHandler(CONSUMER_KEY,CONSUMER_SECRET)
auth1.set_access_token(ACCESS_KEY,ACCESS_SECRET)
api = tweepy.API(auth1)
conn = sqlite3.connect('twitter.db')
cur = conn.cursor()
class StreamListener(tweepy.StreamListener):
status_wrapper = TextWrapper(width=60, initial_indent=' ', subsequent_indent=' ')
def on_status(self, status):
try:
cur.execute('INSERT INTO tweets (text, date) VALUES (?, ?)' ,(status.text,))
print self.status_wrapper.fill(status.text)
print '\n %s %s via %s\n' % (status.author.screen_name, status.created_at, status.source)
conn.commit()
except Exception, e:
print >> sys.stderr, 'Encountered Exception:', e
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
streamer = tweepy.Stream(auth1, StreamListener(), timeout=None)
setTerms = ['news','hello']
streamer.filter(setTerms)