RuntimeError: maximum recursion depth exceeded

递归深度报错

最近在写一些爬虫,按照URL获取html解析,成功后递归:

例:请求def get_detail(mid, page)方法,成功后return get_detail(mid,  page+1),失败则更换代理重新return get_detail(mid, page)

然后长时间运行后出现 RuntimeError: maximum recursion depth exceeded,

之后去stackoverflow查发现是python自身的递归深度是999,也就是最多支持return get_detail 999次,否则就会报错。

可以使用如下解决

import sys

sys.setrecursionlimit(limit) #limit为自定义数目,表示深度

官方文档

sys.setrecursionlimit(limit)

Set the maximum depth of the Python interpreter stack tolimit. This limit prevents infinite recursion from causing an overflow of the C stack and crashing Python.

The highest possible limit is platform-dependent. A user may need to set the limit higher when she has a program that requires deep recursion and a platform that supports a higher limit. This should be done with care, because a too-high limit can lead to a crash.

官方api链接:https://docs.python.org/2/library/sys.html#sys.setrecursionlimit

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 12,768评论 0 33
  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,813评论 0 23
  • 一个人走在路上, 影子被拉得好长好长。 路边和我无关的人群, 熙熙攘攘。 小鸟欢叫在路旁, 我向它靠近, 它扑棱翅...
    大风起兮云飞扬_阅读 141评论 0 1
  • 文/雒宏军 二三十岁的时候,总想着逃离家乡。羡慕城市里的高楼大厦,羡慕城市里的宽阔马路,羡慕城市里的灯红酒绿,总想...
    雒宏军的菜园子阅读 285评论 0 2
  • 听这两首歌感慨颇多 .....又想起雨珊 萌萌 小李。在一起的日子了。哈哈附图噢 小姐姐傻萌傻萌的奥 不好意思 光...
    rosa瑾箬阅读 162评论 1 1