如何使用Tweepy使用Python将使用阿拉伯语文本作为查询进行流式传
发布时间:2020-12-20 13:49:53 所属栏目:Python 来源:网络整理
导读:我一直试图用Tweepy来推送推文.虽然当查询是英文时它工作得很好,但当查询是阿拉伯语时,我无法使它工作.从以前的问题我尝试了twitterStream.filter(track = [u’?????????’])作为unicode,但得到了错误: In[36]: twitterStream.filter(track=[u'?????????'])
我一直试图用Tweepy来推送推文.虽然当查询是英文时它工作得很好,但当查询是阿拉伯语时,我无法使它工作.从以前的问题我尝试了twitterStream.filter(track = [u’?????????’])作为unicode,但得到了错误:
In[36]: twitterStream.filter(track=[u'?????????']) Traceback (most recent call last): File "C:Python27libsite-packagesIPythoncoreinteractiveshell.py",line 3032,in run_code exec(code_obj,self.user_global_ns,self.user_ns) File "<ipython-input-35-706cbc790f1e>",line 1,in <module> Traceback (most recent call last): File "C:Python27libsite-packagesIPythoncoreinteractiveshell.py",line 2978,in run_ast_nodes if self.run_code(code,result): File "C:Python27libsite-packagesIPythoncoreinteractiveshell.py",line 3049,in run_code self.showtraceback() File "C:Program Files (x86)JetBrainsPyCharm 4.5helperspydevpydev_ipython_console_011.py",line 144,in showtraceback import traceback;traceback.print_exc() File "C:Python27libtraceback.py",line 233,in print_exc print_exception(etype,value,tb,limit,file) File "C:Python27libtraceback.py",line 125,in print_exception print_tb(tb,line 70,in print_tb if line: _print(file,' ' + line.strip()) File "C:Python27libtraceback.py",line 13,in _print file.write(str+terminator) File "C:Python27libencodingscp1252.py",line 12,in encode return codecs.charmap_encode(input,errors,encoding_table) UnicodeEncodeError: 'charmap' codec can't encode characters in position 34-42: character maps to <undefined> 如果在这一点上有点绝望,想出这个. 我的代码: __author__ = 'Janis' # -*- coding: utf-8 -*- #!/usr/bin/env python import tweepy impot time # Initialize tweepy functions------------------------------------------------ CONSUMER_KEY ='' CONSUMER_SECRET = '' ACCESS_TOKEN = '' ACCESS_TOKEN_SECRET = '' auth = tweepy.OAuthHandler(CONSUMER_KEY,CONSUMER_SECRET) auth.set_access_token(ACCESS_TOKEN,ACCESS_TOKEN_SECRET) api = tweepy.API(auth) class Listener(tweepy.StreamListener): def on_status(self,status): print status.text return True def on_error(self,status_code): print 'Error in streaming' print status_code pass keyword = 'ISIL' twitterStream =tweepy.Stream(auth=auth,listener=Listener()) twitterStream.filter(track=[u'?????????']) 解决方法
尝试使用twitterStream.filter(track = [unicode(“?????????”,“utf-8”)])
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |