Tweety,UnicodeEncodeError USC-2

时间:2017-08-24 17:38:58

标签: python error-handling tweepy

所以我试图使用python和tweepy包制作一个twitter机器人。 但是,我不断收到UnicodeEncodeError。 发生错误是因为python无法读取/理解传入我通过流提取的推文的表情符号。 我已经用simillar问题搜索了所有其他问题,但是每个发布的解决方案对我给定的代码都不起作用。 我得到的最接近的是使用此处的代码('UCS-2' codec can't encode characters in position 1050-1050

non_bmp_map = dict.fromkeys(range(0x10000, sys.maxunicode + 1), 0xfffd)

然而,即使我能看到一些em

,我仍然会收到错误
    import tweepy
    from unicodedata import normalize, unicodedata
    from tweepy import OAuthHandler
    from tweepy import StreamListener
    from tweepy import Stream
    import sys




    #Variables for each required KEY, secret and token
    consumer_key = ""

    consumer_secret = ""

    access_token = ""

    access_token_secret = ""

    #Set up OAuth and integrate with API
    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)

    auth.set_access_token(access_token, access_token_secret)

    api = tweepy.API(auth)


    class MyStreamListener(tweepy.StreamListener):

        def on_status(self, status):
            print(status.text)

        def on_error(self, status_code):

            if status_code == 420:
                return False

    non_bmp_map = dict.fromkeys(range(0x10000, sys.maxunicode + 1), 0xfffd)
    myStream = tweepy.Stream(auth=api.auth,listener = MyStreamListener())
    start_stream = myStream.filter(track=['Trump'],async=True)

    print(str(start_stream).translate(non_bmp_map))

    #write a tweet to push to account
    #tweet = "I just felt like running!"
    #api.update_status(status=tweet)

这是我收到的错误代码:

Exception in thread Thread-1:
Traceback (most recent call last):
  File "C:\Program Files (x86)\Python36-32\lib\threading.py", line 916, in _bootstrap_inner
    self.run()
  File "C:\Program Files (x86)\Python36-32\lib\threading.py", line 864, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\tweepy\streaming.py", line 294, in _run
    raise exception
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\tweepy\streaming.py", line 263, in _run
    self._read_loop(resp)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\tweepy\streaming.py", line 324, in _read_loop
    self._data(next_status_obj)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\tweepy\streaming.py", line 297, in _data
    if self.listener.on_data(data) is False:
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\tweepy\streaming.py", line 54, in on_data
    if self.on_status(status) is False:
  File "C:\Users\jsilvest\Documents\Python\compugenTwitterBot.py", line 32, in on_status
    print(status.text)
  File "C:\Program Files (x86)\Python36-32\lib\idlelib\run.py", line 345, in write
    return self.shell.write(s, self.tags)
  File "C:\Program Files (x86)\Python36-32\lib\idlelib\rpc.py", line 604, in __call__
    value = self.sockio.remotecall(self.oid, self.name, args, kwargs)
  File "C:\Program Files (x86)\Python36-32\lib\idlelib\rpc.py", line 216, in remotecall
    return self.asyncreturn(seq)
  File "C:\Program Files (x86)\Python36-32\lib\idlelib\rpc.py", line 247, in asyncreturn
    return self.decoderesponse(response)
  File "C:\Program Files (x86)\Python36-32\lib\idlelib\rpc.py", line 267, in decoderesponse
    raise what
UnicodeEncodeError: 'UCS-2' codec can't encode characters in position 138-138: Non-BMP character not supported in Tk

有没有人解决过这个问题?

我正在使用Windows 10并运行Python3

1 个答案:

答案 0 :(得分:1)

答案是......由@Mark Tolonen提供

尝试使用Tweepy时不要使用IDLE

无法支持表情符号。

尝试使用其他IDE,例如PyCharm。我成功使用PyCharm,我相信其他不是IDLE的IDE也会支持它。

谢谢Mark Tolonen