如果发生错误,请重新运行Python脚本
我正在使用他们的流式API抓取twitter数据,有时,我会如果发生错误,请重新运行Python脚本,python,Python,我正在使用他们的流式API抓取twitter数据,有时,我会 File "//swigel_local/Shared/Resources/Tools/Twitter/Streaming/StreamingTwitter.py", line 68, in <module> stream.filter(track=TRACK_TERMS) File "C:\Users\SwigelUser\Anaconda3\lib\site-packages\tweepy\strea
File "//swigel_local/Shared/Resources/Tools/Twitter/Streaming/StreamingTwitter.py", line 68, in <module>
stream.filter(track=TRACK_TERMS)
File "C:\Users\SwigelUser\Anaconda3\lib\site-packages\tweepy\streaming.py", line 445, in filter
self._start(async)
File "C:\Users\SwigelUser\Anaconda3\lib\site-packages\tweepy\streaming.py", line 361, in _start
self._run()
File "C:\Users\SwigelUser\Anaconda3\lib\site-packages\tweepy\streaming.py", line 294, in _run
raise exception
File "C:\Users\SwigelUser\Anaconda3\lib\site-packages\tweepy\streaming.py", line 263, in _run
self._read_loop(resp)
File "C:\Users\SwigelUser\Anaconda3\lib\site-packages\tweepy\streaming.py", line 313, in _read_loop
line = buf.read_line().strip()
AttributeError: 'NoneType' object has no attribute 'strip'
我不知道为什么会出错。但我认为这与我最近所做的改变有关
以前,我在MySQL中的表有utf8排序规则,但这会引起警告,然后我阅读了文章,并将我的表更改为utf8mb4 unicode。
警告消失,程序运行正常。但有时,在程序运行时会出现上述错误
def _read_loop(self, resp):
charset = resp.headers.get('content-type', default='')
enc_search = re.search('charset=(?P<enc>\S*)', charset)
if enc_search is not None:
encoding = enc_search.group('enc')
else:
encoding = 'utf-8'
buf = ReadBuffer(resp.raw, self.chunk_size, encoding=encoding)
while self.running and not resp.raw.closed:
length = 0
while not resp.raw.closed:
line = buf.read_line().strip()
if not line:
self.listener.keep_alive() # keep-alive new lines are expected
elif line.isdigit():
length = int(line)
break
else:
raise TweepError('Expecting length, unexpected value found')
def_read_循环(self,resp):
charset=resp.headers.get('content-type',默认值=“”)
enc_search=re.search('charset=(?P\S*),charset)
如果enc_搜索不是无:
encoding=enc_search.group('enc')
其他:
编码='utf-8'
buf=ReadBuffer(resp.raw,self.chunk\u size,encoding=encoding)
自运行且未响应原始关闭时:
长度=0
虽然没有相应的原始关闭:
line=buf.read_line().strip()
如果不是直线:
self.listener.keep_alive()#keep alive需要新行
elif line.isdigit():
长度=整数(直线)
打破
其他:
raise TweepError('预期长度,找到意外值')
有没有一种方法可以在发生此错误时自动重新运行程序
您可以编写一个外部应用程序来监视系统,检测应用程序的崩溃并重新启动它
但是我不推荐它
正确的解决办法是:
- 确定此问题的原因并解决它李>
- 使用
,捕获此类错误(例如,try.包装有问题的代码段。。除了
)并相应地处理它们属性错误
None
值的原因,或者执行None
检查(或错误),但不执行该代码,或者修复潜在的逻辑错误。我只是无法确定问题的原因,您能帮我解决吗?@Dylan,查看错误:AttributeError:'NoneType'对象没有属性'strip'
。您可以看到,它表示您从None
值调用strip
。由于您正在调用buf.read\u line().strip()
,这意味着buf.read\u line()
返回None
。我不知道什么是buf
,但它可能是说EOF
的方式。阅读该库的手册,并在它抛出属性时进行检查。谢谢你的建议,我去了该库并将相关行粘贴到了我的原始帖子中。我可以修改库中的代码吗?@Dylan。首先,我想更正我的最后一条评论:检查buf.read_line()
何时返回None
,而不是在它引发AttributeError
时。我看到了新代码。我不知道什么是ReadBuffer
,但是您应该检查它的read\u line()
方法何时返回None
。另一种修复方法可能是将循环更改为line=buf.read_line();而line:line=line.strop()
def _read_loop(self, resp):
charset = resp.headers.get('content-type', default='')
enc_search = re.search('charset=(?P<enc>\S*)', charset)
if enc_search is not None:
encoding = enc_search.group('enc')
else:
encoding = 'utf-8'
buf = ReadBuffer(resp.raw, self.chunk_size, encoding=encoding)
while self.running and not resp.raw.closed:
length = 0
while not resp.raw.closed:
line = buf.read_line().strip()
if not line:
self.listener.keep_alive() # keep-alive new lines are expected
elif line.isdigit():
length = int(line)
break
else:
raise TweepError('Expecting length, unexpected value found')