Twitter流式传输在Python中:cp949编码

2024-04-16 05:22:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在使用tweepy收集数据流API。你知道吗

这是我的代码,我在Acaconda命令提示符下运行了这个。当流媒体开始时,它返回tweets,然后在发出少量tweets后,它会给出以下错误:

Streaming Started ...
RT @ish10040: Crack Dealer Released Early From Prison By Obama Murders Woman And Her 2 Young Kids… Exception in thread Thread-1:
Traceback (most recent call last):
  File "C:\Users\Jae Hee\Anaconda2\lib\threading.py", line 801, in __bootstrap_inner
    self.run()
  File "C:\Users\Jae Hee\Anaconda2\lib\threading.py", line 754, in run
    self.__target(*self.__args, **self.__kwargs)
  File "C:\Users\Jae Hee\Anaconda2\lib\site-packages\tweepy\streaming.py", line 294, in _run
    raise exception
UnicodeEncodeError: 'cp949' codec can't encode character u'\xab' in position 31: illegal multibyte sequence

我相信这与编码有关,所以我使用chcp65001来处理这个问题,但它没有给出解决方案!你知道吗

这是密码

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):
        #returning False in on_data disconnects the stream
        if status_code == 420:
            return False

def main():

    myStreamListener = MyStreamListener()
    myStream = tweepy.Stream(auth = api.auth, listener = myStreamListener)


    print "Streaming Started ..."

    try:
        myStream.filter(track=['Obama'], async = True)
    except:
        print "error!"
        myStream.disconnect()

if __name__ == '__main__':
    main()

Tags: runinpyselfauthaccesslibstatus
1条回答
网友
1楼 · 发布于 2024-04-16 05:22:45

通过twitterapi生成和接受的所有文本都应该编码为UTF-8,因此您的代码应该使用该编解码器来解码返回的内容。你知道吗

请看这里:https://dev.twitter.com/overview/api/counting-characters

相关问题 更多 >