gpt4 book ai didi

python - 如何使用 Tweepy 使用阿拉伯语文本作为使用 Python 的查询来流式传输推文?

转载 作者:太空宇宙 更新时间:2023-11-03 11:49:55 25 4
gpt4 key购买 nike

我一直在尝试使用 Tweepy 流式传输推文。虽然当查询为英语时它工作得很好,但当查询为阿拉伯语时它无法正常工作。根据之前的问题,我尝试将 twitterStream.filter(track=[u'الإسلامي٩']) 作为 unicode 但出现错误:

In[36]: twitterStream.filter(track=[u'الإسلامية'])

Traceback (most recent call last):
File "C:\Python27\lib\site-packages\IPython\core\interactiveshell.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:\Python27\lib\site-packages\IPython\core\interactiveshell.py", line 2978, in run_ast_nodes
if self.run_code(code, result):
File "C:\Python27\lib\site-packages\IPython\core\interactiveshell.py", line 3049, in run_code
self.showtraceback()
File "C:\Program Files (x86)\JetBrains\PyCharm 4.5\helpers\pydev\pydev_ipython_console_011.py", line 144, in showtraceback
import traceback;traceback.print_exc()
File "C:\Python27\lib\traceback.py", line 233, in print_exc
print_exception(etype, value, tb, limit, file)
File "C:\Python27\lib\traceback.py", line 125, in print_exception
print_tb(tb, limit, file)
File "C:\Python27\lib\traceback.py", line 70, in print_tb
if line: _print(file, ' ' + line.strip())
File "C:\Python27\lib\traceback.py", line 13, in _print
file.write(str+terminator)
File "C:\Python27\lib\encodings\cp1252.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
import 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")])

关于python - 如何使用 Tweepy 使用阿拉伯语文本作为使用 Python 的查询来流式传输推文?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30311902/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com