问题描述
在运行此程序以使用 Python 2.7.8 检索 Twitter 数据时:
While running this program to retrieve Twitter data using Python 2.7.8 :
#imports
from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
#setting up the keys
consumer_key = '…………...'
consumer_secret = '………...'
access_token = '…………...'
access_secret = '……………..'
class TweetListener(StreamListener):
# A listener handles tweets are the received from the stream.
#This is a basic listener that just prints received tweets to standard output
def on_data(self, data):
print (data)
return True
def on_error(self, status):
print (status)
#printing all the tweets to the standard output
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
stream = Stream(auth, TweetListener())
t = u"سوريا"
stream.filter(track=[t])
运行该程序 5 小时后,我收到此错误消息:
after running this program for 5 hours i got this Error message:
Traceback (most recent call last):
File "/Users/Mona/Desktop/twitter.py", line 32, in <module>
stream.filter(track=[t])
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/tweepy/streaming.py", line 316, in filter
self._start(async)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/tweepy/streaming.py", line 237, in _start
self._run()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/tweepy/streaming.py", line 173, in _run
self._read_loop(resp)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/tweepy/streaming.py", line 225, in _read_loop
next_status_obj = resp.read( int(delimited_string) )
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 543, in read
return self._read_chunked(amt)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 612, in _read_chunked
value.append(self._safe_read(chunk_left))
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 660, in _safe_read
raise IncompleteRead(''.join(s), amt)
IncompleteRead: IncompleteRead(0 bytes read, 976 more expected)
>>>
其实我不知道怎么解决这个问题!!!
Actually i don't know what to do with this problem !!!
推荐答案
您应该检查是否未能使用 stall_warnings
参数.
You should check to see if you're failing to process tweets quickly enough using the stall_warnings
parameter.
stream.filter(track=[t], stall_warnings=True)
这些消息由 Tweepy 处理(查看实施此处) 并会在您落后时通知您.落后意味着您无法像 Twitter API 向您发送推文那样快速处理推文.来自 Twitter 文档:
These messages are handled by Tweepy (check out implementation here) and will inform you if you're falling behind. Falling behind means that you're unable to process tweets as quickly as the Twitter API is sending them to you. From the Twitter docs:
如果客户端有断开连接的危险,将此参数设置为字符串 true 将导致定期发送消息.这些消息仅在客户端落后时发送,并且以大约每 5 分钟一次的最大速率发生.
理论上,您应该会收到来自 API 的断开连接消息在这种情况下.然而,情况并非总是如此:
In theory, you should receive a disconnect message from the API in this situation. However, that is not always the case:
流媒体 API 将尝试传递一条消息,指出流为何被关闭.请注意,如果断开连接是由于网络问题或客户端读取速度过慢,则可能无法收到此消息.
IncompleteRead
也可能是临时网络问题造成的,以后可能再也不会发生.不过,如果它在大约 5 小时后重现,那么落后是一个不错的选择.
The IncompleteRead
could also be due to a temporary network issue and may never happen again. If it happens reproducibly after about 5 hours though, falling behind is a pretty good bet.
这篇关于“未完成阅读"使用 Python 检索 Twitter 数据时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!