为什么使用Python 3的Tweepy根本不会从流中返回任何数据?(Why does Tweepy with Python 3 not return any data at all from a s

编程入门 行业动态 更新时间:2024-10-28 03:26:32
为什么使用Python 3的Tweepy根本不会从流中返回任何数据?(Why does Tweepy with Python 3 not return any data at all from a stream?)

Python 3.5.2,Tweepy 3.5.0,Windows 8.1

我正在关注senddex制作的教程,该教程展示了如何使用tweepy从Twitter流式传输数据。 (他的教程是在Python 2中,但Python 3很容易)

但是,当我运行脚本时,它不会吐出任何数据。 它会挂起,直到我得到一个3路IncompleteRead异常,或直到我执行Ctrl + C.

这是我的Listener类代码:

class listener(StreamListener): def on_date(self,data): try: print(data) save = open('twitDB.csv', 'a') save.write(data) save.write('\n') save.close() return True except BaseException as e: print('failed on data,',str(e)) time.sleep(5) def on_error(self,status): print(status) auth = OAuthHandler(ckey, csecret) auth.set_access_token(atoken,asecret) twitterStream = Stream(auth=auth, listener=listener()) twitterStream.filter(track=["car"])

正如你所看到的,我将它设置为捕获错误并打印数据,同时将其保存到csv,但它并没有真正做任何事情,只是挂起。

此外,对于赛道,我确实尝试使用不那么普通的东西,但它仍然被绞死。

引发KeyboardInterrupt时:

Traceback (most recent call last): File "C:\Program Files\Python35\lib\site-packages\requests\packages\urllib3\co ntrib\pyopenssl.py", line 217, in recv_into return self.connection.recv_into(*args, **kwargs) File "C:\Program Files\Python35\lib\site-packages\OpenSSL\SSL.py", line 1352, in recv_into self._raise_ssl_error(self._ssl, result) File "C:\Program Files\Python35\lib\site-packages\OpenSSL\SSL.py", line 1167, in _raise_ssl_error raise WantReadError() OpenSSL.SSL.WantReadError During handling of the above exception, another exception occurred: Traceback (most recent call last): File "twittertest.py", line 33, in <module> twitterStream.filter(track=["car"]) File "C:\Program Files\Python35\lib\site-packages\tweepy\streaming.py", line 4 45, in filter self._start(async) File "C:\Program Files\Python35\lib\site-packages\tweepy\streaming.py", line 3 61, in _start self._run() File "C:\Program Files\Python35\lib\site-packages\tweepy\streaming.py", line 2 63, in _run self._read_loop(resp) File "C:\Program Files\Python35\lib\site-packages\tweepy\streaming.py", line 3 13, in _read_loop line = buf.read_line().strip() File "C:\Program Files\Python35\lib\site-packages\tweepy\streaming.py", line 1 79, in read_line self._buffer += self._stream.read(self._chunk_size) File "C:\Program Files\Python35\lib\site-packages\requests\packages\urllib3\re sponse.py", line 310, in read data = self._fp.read(amt) File "C:\Program Files\Python35\lib\http\client.py", line 448, in read n = self.readinto(b) File "C:\Program Files\Python35\lib\http\client.py", line 478, in readinto return self._readinto_chunked(b) File "C:\Program Files\Python35\lib\http\client.py", line 573, in _readinto_ch unked chunk_left = self._get_chunk_left() File "C:\Program Files\Python35\lib\http\client.py", line 541, in _get_chunk_l eft chunk_left = self._read_next_chunk_size() File "C:\Program Files\Python35\lib\http\client.py", line 501, in _read_next_c hunk_size line = self.fp.readline(_MAXLINE + 1) File "C:\Program Files\Python35\lib\socket.py", line 575, in readinto return self._sock.recv_into(b) File "C:\Program Files\Python35\lib\site-packages\requests\packages\urllib3\co ntrib\pyopenssl.py", line 230, in recv_into [self.socket], [], [], self.socket.gettimeout()) KeyboardInterrupt

这是我第一次使用社交媒体API,所以如果我遗漏了一些明显的东西,我会道歉。 感谢帮助,谢谢。

Python 3.5.2, Tweepy 3.5.0, Windows 8.1

I'm following a tutorial made by sentdex that shows how to stream data from Twitter using tweepy. (His tutorial is in Python 2 but it is pretty easy to Python 3-ify it)

However, when I run the script, it doesn't spit out any data. It hangs until I get a 3-way IncompleteRead exception, or until I do Ctrl+C.

Here is my Listener class code:

class listener(StreamListener): def on_date(self,data): try: print(data) save = open('twitDB.csv', 'a') save.write(data) save.write('\n') save.close() return True except BaseException as e: print('failed on data,',str(e)) time.sleep(5) def on_error(self,status): print(status) auth = OAuthHandler(ckey, csecret) auth.set_access_token(atoken,asecret) twitterStream = Stream(auth=auth, listener=listener()) twitterStream.filter(track=["car"])

As you can see, I have it set up to catch errors and print data out while saving it to a csv, but it doesn't really do anything, just hangs.

Also, for track, I did try to use something less general but it still hanged.

When KeyboardInterrupt is raised:

Traceback (most recent call last): File "C:\Program Files\Python35\lib\site-packages\requests\packages\urllib3\co ntrib\pyopenssl.py", line 217, in recv_into return self.connection.recv_into(*args, **kwargs) File "C:\Program Files\Python35\lib\site-packages\OpenSSL\SSL.py", line 1352, in recv_into self._raise_ssl_error(self._ssl, result) File "C:\Program Files\Python35\lib\site-packages\OpenSSL\SSL.py", line 1167, in _raise_ssl_error raise WantReadError() OpenSSL.SSL.WantReadError During handling of the above exception, another exception occurred: Traceback (most recent call last): File "twittertest.py", line 33, in <module> twitterStream.filter(track=["car"]) File "C:\Program Files\Python35\lib\site-packages\tweepy\streaming.py", line 4 45, in filter self._start(async) File "C:\Program Files\Python35\lib\site-packages\tweepy\streaming.py", line 3 61, in _start self._run() File "C:\Program Files\Python35\lib\site-packages\tweepy\streaming.py", line 2 63, in _run self._read_loop(resp) File "C:\Program Files\Python35\lib\site-packages\tweepy\streaming.py", line 3 13, in _read_loop line = buf.read_line().strip() File "C:\Program Files\Python35\lib\site-packages\tweepy\streaming.py", line 1 79, in read_line self._buffer += self._stream.read(self._chunk_size) File "C:\Program Files\Python35\lib\site-packages\requests\packages\urllib3\re sponse.py", line 310, in read data = self._fp.read(amt) File "C:\Program Files\Python35\lib\http\client.py", line 448, in read n = self.readinto(b) File "C:\Program Files\Python35\lib\http\client.py", line 478, in readinto return self._readinto_chunked(b) File "C:\Program Files\Python35\lib\http\client.py", line 573, in _readinto_ch unked chunk_left = self._get_chunk_left() File "C:\Program Files\Python35\lib\http\client.py", line 541, in _get_chunk_l eft chunk_left = self._read_next_chunk_size() File "C:\Program Files\Python35\lib\http\client.py", line 501, in _read_next_c hunk_size line = self.fp.readline(_MAXLINE + 1) File "C:\Program Files\Python35\lib\socket.py", line 575, in readinto return self._sock.recv_into(b) File "C:\Program Files\Python35\lib\site-packages\requests\packages\urllib3\co ntrib\pyopenssl.py", line 230, in recv_into [self.socket], [], [], self.socket.gettimeout()) KeyboardInterrupt

It's my first time going with a social media API, so I apologize if I'm missing something obvious. Help would be appreciated, thanks.

最满意答案

def on_date(self,data):

这应该是

def on_data(self,data): def on_date(self,data):

This should have been

def on_data(self,data):

更多推荐

本文发布于:2023-08-03 13:51:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1390854.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据   Tweepy   Python   stream   data

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!