PythonTwitterAPI流tweepy试图将数据保存到CSV文件
上述代码的问题在于,它不断出现一条错误消息,指出//def on_error//row出现意外的未登录。您打开了一个PythonTwitterAPI流tweepy试图将数据保存到CSV文件,python,api,csv,twitter,Python,Api,Csv,Twitter,上述代码的问题在于,它不断出现一条错误消息,指出//def on_error//row出现意外的未登录。您打开了一个try块,但未捕获异常 还要注意,python区分大小写,因此saveFile不是saveFile,也不是saveFile.write()是saveFile.write() 按如下方式编辑\u data()处理程序的,应该可以使其正常工作: from tweepy import Stream from tweepy import OAuthHandler from tweepy.
try
块,但未捕获异常
还要注意,python区分大小写,因此saveFile
不是saveFile
,也不是saveFile.write()
是saveFile.write()
按如下方式编辑\u data()处理程序的,应该可以使其正常工作:
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_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)
打开try
块而不捕获异常
还要注意,python区分大小写,因此saveFile
不是saveFile
,也不是saveFile.write()
是saveFile.write()
按如下方式编辑\u data()
处理程序的,应该可以使其正常工作:
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_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)
一条错误消息,指出意外缩进仍然存在“class listener(StreamListener):def on_data(self,data):try:print(data),open('TwitterAPI.csv','a')作为f:f.write(data),Exception作为e:#这里捕获您可能遇到的任何异常。print(“[!]错误:%s“%e”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”])'很抱歉,我是python新手,以前从未使用过它。在您告诉我插入新脚本的部分中,它声明它是无效的syntaxclass侦听器(StreamListener):def on_data(self,data):try:print(data),open('TwitterAPI.csv','a')作为f:f.write(data)例外情况除外,如e:#这里捕捉您可能遇到的任何例外情况。print(“[!]错误:%s“%e”)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”])错误仅在运行后出现这不是错误消息这是您的源代码。我需要的是错误消息,而不是代码一条错误消息,指出意外的缩进仍然存在“类侦听器(StreamListener):在_数据(self,data)上定义:try:print(data)并将open('TwitterAPI.csv','a')作为f:f.write(data)例外情况除外,如e:#这里捕捉您可能遇到的任何例外情况。print(“[!]错误:%s“%e”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”])'很抱歉,我是python新手,以前从未使用过它。在您告诉我插入新脚本的部分中,它声明它是无效的syntaxclass侦听器(StreamListener):def on_data(self,data):try:print(data),open('TwitterAPI.csv','a')作为f:f.write(data)例外情况除外,如e:#这里捕捉您可能遇到的任何例外情况。print(“[!]错误:%s“%e”)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”])错误仅在运行后出现这不是错误消息这是您的源代码。我需要的是错误信息,不是代码