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 中的一个已知错误有关。取自那里的post。
在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'
我想打印出其中包含#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 中的一个已知错误有关。取自那里的post。
在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'