TypeError:无法转换'字节'隐含地反对str

时间:2014-11-20 12:15:07

标签: python twitter tweepy

我想使用tweepy API来传输来自tweeter的数据,我使用这个视频(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

2 个答案:

答案 0 :(得分:1)

我更新了API并重新安装了! 有人提高了。 它现在有效:)

答案 1 :(得分:1)

想到我与我分享我的经验。我的一个朋友用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')