Tweepy filter utf-8 encoding

2019-05-30 08:51发布

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条回答
在下西门庆
2楼-- · 2019-05-30 09:09

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.

查看更多
登录 后发表回答