Python3 + Tweepy流式错误

时间:2015-10-22 22:33:33

标签: python-3.x error-handling tweepy

我想打印出包含#Berlin主题标签的推文。如何重写代码?我无法在python3中找到此操作的示例代码。 我有以下问题:

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

    consumer_key = ''
    consumer_secret = ''
    access_token = ''
    access_token_secret = ''

    #This is a basic listener that just prints received tweets to stdout.
    class StdOutListener(StreamListener):

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

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


    if __name__ == '__main__':

        #This handles Twitter authetification and the connection to Twitter Streaming API
        l = StdOutListener()
        auth = OAuthHandler(consumer_key, consumer_secret)
        auth.set_access_token(access_token, access_token_secret)
        stream = Stream(auth, l)

        #This line filter Twitter Streams to capture data by the keywords: 'python', 'javascript', 'ruby'
        stream.filter(track=['Berlin'])

然后我在最后得到了这个错误:

Traceback (most recent call last):
  File "test.py", line 31, in <module>
    stream.filter(track=['Berlin'])
  File "/home/ubuntu/tweepy/tweepy/streaming.py", line 430, in filter
    self._start(async)
  File "/home/ubuntu/tweepy/tweepy/streaming.py", line 346, in _start
    self._run()
  File "/home/ubuntu/tweepy/tweepy/streaming.py", line 286, in _run
    raise exception
  File "/home/ubuntu/tweepy/tweepy/streaming.py", line 255, in _run
    self._read_loop(resp)
  File "/home/ubuntu/tweepy/tweepy/streaming.py", line 298, in _read_loop
    line = buf.read_line().strip()
  File "/home/ubuntu/tweepy/tweepy/streaming.py", line 171, in read_line
    self._buffer += self._stream.read(self._chunk_size)
TypeError: Can't convert 'bytes' object to str implicitly

1 个答案:

答案 0 :(得分:1)

这与tweepy #615中的已知错误有关。取自那里的帖子。

在streaming.py中:

我将第161行更改为

self._buffer += self._stream.read(read_len).decode('UTF-8', 'ignore') 

和第171行到

self._buffer += self._stream.read(self._chunk_size).decode('UTF-8', 'ignore')

他们需要在Windows上更改的文件位于\Python 3.5\Lib\site-packages\tweepy下。

对于Ubuntu,您需要:'/usr/lib/python3.5/dist-packages/tweepy'