【问题标题】:Python3 + Tweepy streaming ERRORPython3 + Tweepy 流错误
【发布时间】:2015-10-22 22:33:33
【问题描述】:

我想打印出包含#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

【问题讨论】:

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


    【解决方案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'

    【讨论】:

    • 我有 python 3.4 和 2.7 并且找不到这个路径。如何在 2.7 下解决这个问题? @Leb
    • 你有什么操作系统? python版本无关紧要。
    • 我有 ubuntu 14.04 桌面,无论如何我发现当我删除 pip requests[security] 时(我安装它是因为 python 2.7 控制台中的 InsecurePlatformWarning 消息)现在似乎工作正常.. @莱布
    猜你喜欢
    • 2022-06-15
    • 2018-06-23
    • 1970-01-01
    • 2013-12-29
    • 2012-08-21
    • 2017-06-04
    • 2018-05-30
    • 1970-01-01
    • 2012-08-26
    相关资源
    最近更新 更多