gpt4 book ai didi

python-3.x - Python3 + Tweepy流式传输错误

转载 作者:行者123 更新时间:2023-12-03 07:54:07 25 4
gpt4 key购买 nike

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

关于python-3.x - Python3 + Tweepy流式传输错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33291889/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com