【问题标题】:TypeError: Can't convert 'bytes' object to str implicitlyTypeError:无法将“字节”对象隐式转换为 str
【发布时间】:2015-01-18 07:13:25
【问题描述】:

我想使用 tweepy API 从高音扬声器流式传输数据,我使用这个视频 (http://sentdex.com/sentiment-analysisbig-data-and-python-tutorials-algorithmic-trading/how-to-use-the-twitter-api-1-1-to-stream-tweets-in-python/) 来学习如何做到这一点,但不幸的是我收到了这个错误,

from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener

ckey = 'credentials'
csecret = 'you'
atoken = 'should'
asecret = 'invalidate'

class listener(StreamListener):

    def on_data(self, data):
        print (data)
        return True

    def on_error(self, status):
        print (status)

auth = OAuthHandler(ckey, csecret)
auth.set_access_token(atoken, asecret)
twitterStream = Stream(auth, listener())
twitterStream.filter(track=["car"])

这是错误:

Traceback (most recent call last):
  File "C:/Users/azamb/PycharmProjects/PyStream/Stream.py", line 24, in <module>
    twitterStream.filter(track=["car"])
  File "C:\Python34\lib\site-packages\tweepy-2.3-py3.4.egg\tweepy\streaming.py", line 418, in filter
  File "C:\Python34\lib\site-packages\tweepy-2.3-py3.4.egg\tweepy\streaming.py", line 335, in _start
  File "C:\Python34\lib\site-packages\tweepy-2.3-py3.4.egg\tweepy\streaming.py", line 275, in _run
  File "C:\Python34\lib\site-packages\tweepy-2.3-py3.4.egg\tweepy\streaming.py", line 244, in _run
  File "C:\Python34\lib\site-packages\tweepy-2.3-py3.4.egg\tweepy\streaming.py", line 287, in _read_loop
  File "C:\Python34\lib\site-packages\tweepy-2.3-py3.4.egg\tweepy\streaming.py", line 167, in read_line
TypeError: Can't convert 'bytes' object to str implicitly

【问题讨论】:

  • 说真的,您的凭据?
  • 这是那个库的问题;它的 Python 3 支持不是很好。你应该提出来there
  • 我使用的是 Python 3.4 而不是 3.3

标签: python twitter tweepy


【解决方案1】:

我更新了 API 并再次安装了它! 有人对此进行了改进。 现在可以了:)

【讨论】:

    【解决方案2】:

    只是想我会分享我的经验。我的一个朋友正在处理与 tweepy 类似的问题。问题出在stream.py 文件(Python 抛出错误的地方)。这可能是旧 tweepy 版本的问题,但在该文件中,read_lineread_len 函数有缺陷。 .decode('utf-8') 必须手动添加,如下所示:

    def read_len(self, length):
            while not self._stream.closed:
                if len(self._buffer) >= length:
                    return self._pop(length)
                read_len = max(self._chunk_size, length - len(self._buffer))
                # #####
                # HERE 
                # #####
                self._buffer += self._stream.read(read_len).decode('utf-8')
    
    def read_line(self, sep='\n'):
        start = 0
        while not self._stream.closed:
            loc = self._buffer.find(sep, start)
            if loc >= 0:
                return self._pop(loc + len(sep))
            else:
                start = len(self._buffer)
            # #########
            # AND HERE 
            # #########
            self._buffer += self._stream.read(self._chunk_size).decode('utf-8')
    

    【讨论】:

      猜你喜欢
      • 2013-05-17
      • 1970-01-01
      • 2017-08-31
      • 2012-11-19
      • 2015-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多