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,所以如果我遗漏了一些明显的东西,我会道歉。感谢帮助,谢谢。
答案 0 :(得分:1)
def on_date(self,data):
这应该是
def on_data(self,data):
答案 1 :(得分:0)
没关系,当我将on_status与status.text一起使用时,它确实有用,必须是我所遗漏的东西。