我下面的脚本(取自各种在线资源)没有写入数据库。我没有得到任何错误,如果我注释掉数据库行,那么它将毫无问题地输出到控制台。在
数据库存在,它有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)
是否要执行此操作:
或者这个:
^{pr2}$在第一种情况下,
cursor
未声明。在而且,正如lqc指出的,你可以让所有的错误保持沉默。不要捕捉任何异常来查看发生了什么,或者将其更改为更具体的内容(如
UnicodeError
)。在首先,修复你的压痕。你没有得到任何错误,因为你明确地让他们闭嘴!与
这将捕获
try: except:
块中发生的任何异常。阅读Python教程是一个good start to learn more about exceptions.相关问题 更多 >
编程相关推荐