from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
ckey = 'hidden due to question'
csecret = 'hidden due to question'
atoken = 'hidden due to question'
asecret = 'hidden due to question'
class listener(StreamListener):
def on_data(self, data):
try:
print (data)
saveFile = open('TwitterAPI.csv','a')
saveFile.write(data)
saveFile.Write('\n')
saveFIle.close()
return (True)
def on_error(self, status):
print (status)
auth = OAuthHandler(ckey, csecret)
auth.set_access_token(atoken, asecret)
twitterStream = Stream(auth, listener())
twitterStream.filter(track=["car"])
上面代码的问题在于它不断出现一条错误消息,指出// def on_error //行
出现意外的意外情况答案 0 :(得分:0)
您打开$array = array(
0 => 1,
1 => 3,
2 => 7,
3 => 0,
4 => 6,
5 => 3,
);
块而不捕获异常。
https://docs.python.org/3/tutorial/errors.html
另外要小心,python区分大小写,因此try
不是saveFile
,saveFIle
也不是saveFile.write()
...
按照以下方式编辑saveFile.Write()
处理程序应该可以正常运行:
on_data()
修改:以下是您重新编写的完整代码:
def on_data(self, data):
try:
print(data)
with open('TwitterAPI.csv','a') as f:
f.write(data)
except Exception as e: # here catch whatever exception you may have.
print('[!] Error : %s' % e)