我想使用Twitter
和Python3.5
从Tweepy
检索阿拉伯数据。
我发现一个程序可以很好地使用英语或法语。但是对于阿拉伯语,推文是解码的。 对于exp:
\ u04f \ u04e \ u043e \ u0430 \ U0430 \ u044f
这是该计划:
from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
ConsumerKey = 'Your_Consumer_Key'
ConsumerSecret = 'Your_Consumer_Secret'
AccessToken = 'Your_Access_Token'
AccessTokenSecret = 'Your_Access_Token_Secret'
tweets = open('Tweets.txt',mode='w',encoding="utf8",newline=None)
class listener(StreamListener) :
def on_data (self , data) :
tweets.write(data)
print (data)
return True
def on_error (self , status) :
print (status)
auth = OAuthHandler (ConsumerKey , ConsumerSecret)
auth.set_access_token(AccessToken , AccessTokenSecret)
twitterStream = Stream(auth , listener())
twitterStream.filter(track=['أحوال','الطقس','2016'])
tweets.close()
我使用了一些功能,但是我收到了错误:
答案 0 :(得分:0)
该代码效果很好
from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
import json
import sys
ConsumerKey = 'Your_Consumer_Key'
ConsumerSecret = 'Your_Consumer_Secret'
AccessToken = 'Your_Access_Token'
AccessTokenSecret = 'Your_Access_Token_Secret'
non_bmp_map = dict.fromkeys(range(0x10000, sys.maxunicode + 1), 0xfffd)
#tweets = open('Tweets.txt',mode='w',encoding="utf8",newline=None)
class listener(StreamListener) :
def on_data (self , data):
try:
tweet = json.loads(data)['text']
print(tweet.translate(non_bmp_map))
except KeyError:
pass
def on_error(self , status):
print(status)
auth = OAuthHandler (ConsumerKey , ConsumerSecret)
auth.set_access_token(AccessToken , AccessTokenSecret)
twitterStream = Stream(auth , listener())
twitterStream.filter(track=['الله'])
#tweets.close()