簡體   English   中英

Python3 + Tweepy流式傳輸錯誤

[英]Python3 + Tweepy streaming ERROR

我想打印出其中帶有#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

這與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'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM