Tweepy filter utf-8 encoding

一世执手 提交于 2019-12-23 04:26:28

问题


I have been working with the Tweepy and when I execute the filter of the tweepy passing non-ascii characters I have an error. For exemple, using the following command, I got the error below:

My code:

auth = tweepy.OAuthHandler(apikey[0], apikey[1])
auth.set_access_token(apikey[2], apikey[3])
api = tweepy.API(auth)
sapi = tweepy.streaming.Stream(auth, CustomStreamListener())
sapi.filter(track=['eleições'])

My error:

Traceback (most recent call last):
  File "./TwitterStreamingAPI.py", line 81, in <module>
    sapi.filter(track=['eleições']) 
  File "/usr/local/lib/python2.7/dist-packages/tweepy/streaming.py", line 303, in filter
    encoded_track = [s.encode(encoding) for s in track]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe7 in position 4: ordinal not in range(128)

I would be glad if someone help me in how to solve it.

Thanks in advance,

Thiago.


回答1:


Try replacing the filter line with:

sapi.filter(track=[u'eleições'])

One does .encode() to convert from unicode to str. One does .decode() to convert from str to unicode. Since tweepy is attempting to .encode(), we should feed it a unicode.



来源:https://stackoverflow.com/questions/25007898/tweepy-filter-utf-8-encoding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!