我想用所有相关数据保存阿拉伯语推文,例如推文ID,用户位置,用户关注者等。
from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
import json
import sys
import codecs
import time
ConsumerKey = ' '
ConsumerSecret = ' '
AccessToken = ' '
AccessTokenSecret = ' '
non_bmp_map = dict.fromkeys(range(0x10000, sys.maxunicode + 1), 0xfffd)
class StdOutListener(StreamListener):
def on_data(self, data):
try:
all_data = json.loads(data)
tweet = all_data["text"]
print(tweet.translate(non_bmp_map))
SaveFile = codecs.open('ArabictwitDB.txt','a', "utf-8")
SaveFile.write(all_data)
SaveFile.write('\n')
SaveFile.close()
return True
except BaseException, e:
print 'failed',str(e)
time.sleep(5)
def on_error(self, status):
print status
if __name__ == '__main__':
l = StdOutListener()
auth = OAuthHandler(ConsumerKey, ConsumerSecret)
auth.set_access_token(AccessToken, AccessTokenSecret)
stream = Stream(auth, l)
stream.filter(track=[unicode("رمضان","utf-8")])
它显示了我的错误
failed coercing to Unicode: need string or buffer, dict found
但如果我替换all_data
SaveFile.write(all_data)
使用推文
SaveFile.write(tweet)
它只保存阿拉伯语文本 如何将所有数据保存到文件中?
答案 0 :(得分:0)
如果您不需要格式化,只需
SaveFile.write(json.dumps(all_data))
但是因为你没有改变任何东西..为什么不只是保存data
呢?
或者您可以像使用tweet