Python的最大递归深度错误 “maximum recursion depth exceeded while calling a Python object”

今天在写爬虫的时候,发现了一个诡异的事情,使用str方法强制转换一个BeautifulSoup对象成字符串的时候报错了,提示是“maximum recursion depth exceeded while calling a Python object”,意思大致是“当调用该对象超过最大递归深度”

报错如下:

  Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\site-packages\bs4\element.py", line 1045, in __str__
    return self.encode()
  File "C:\Python27\lib\site-packages\bs4\element.py", line 1055, in encode
    u = self.decode(indent_level, encoding, formatter)
  File "C:\Python27\lib\site-packages\bs4\element.py", line 1126, in decode
    indent_contents, eventual_encoding, formatter)
  File "C:\Python27\lib\site-packages\bs4\element.py", line 1195, in decode_contents
    formatter))
  File "C:\Python27\lib\site-packages\bs4\element.py", line 1126, in decode
  ......
  
  File "C:\Python27\lib\site-packages\bs4\element.py", line 1126, in decode
    indent_contents, eventual_encoding, formatter)
  File "C:\Python27\lib\site-packages\bs4\element.py", line 1195, in decode_contents
    formatter))
  File "C:\Python27\lib\site-packages\bs4\element.py", line 1098, in decode
    text = self.format_string(val, formatter)
  File "C:\Python27\lib\site-packages\bs4\element.py", line 163, in format_string
    output = formatter(s)
  File "C:\Python27\lib\site-packages\bs4\element.py", line 120, in substitute_xml
    ns, EntitySubstitution.substitute_xml)
  File "C:\Python27\lib\site-packages\bs4\element.py", line 104, in _substitute_if_appropriate
    if (isinstance(ns, NavigableString)
RuntimeError: maximum recursion depth exceeded while calling a Python object

而后更神奇的是我使用的ptpython并没有报错,直接通过了。

其实原因是在Python里的递归调用是有限制的,可以使用sys模块里的getrecursionlimit方法查看的到,即(想深入的同学可以谷歌上搜索一番,这里提供笔者所搜索到的https://cyrusin.github.io/2015/12/08/python-20151208/

sys.getrecursionlimit()

打开终端运行Python,可以看到默认限制值为1000

而ptpython里默认限制值为2000,这也不难解释为什么python下直接运行会报最大深度递归错误而ptpython可以正常运行了。

那么该来解决这个问题了,有get自然有set(当然还有其他方法比如达到深度限制时就做对应处理这方面不符合笔者目前需求,所以就不赘述,有需求的同学请自行谷歌百度一下),那么设置最大深度限制的方法就是setrecursionlimit了,至于设置值为多少你自行设置了

sys.setrecursionlimit(2000)

至此,问题解决!

时间: 2024-08-07 06:42:35

Python的最大递归深度错误 “maximum recursion depth exceeded while calling a Python object”的相关文章

Odoo8查询产品时提示&quot;maximum recursion depth exceeded while calling a Python object&quot;

今天在生产系统中查询产品时,莫名提示错误:maximum recursion depth exceeded while calling a Python object,根据错误日志提示,发现在查询产品时,系统会构造一个domain,查询所有库位的库存量.当仓库较多的时候,构造的这个domain比较长,然后解析这个domain的方法distribute_negate是递归调用,因为递归次数太多,所以就提示错误. 根据源码查看了生成domain的条件,这个部分不太好调整,所以后来直接找了个方法来增加

pyinstaller执行后出现maximum recursion depth exceeded while calling a Python object

通过查阅,得知如下: 1.递归深度不够,我设置一下递归深度 # 在首文件头部import sys sys.setrecursionlimit(5000) 2.openpyxl的问题 # openpyxl版本问题,在2.3.5可以正常打包 pip uninstall openpyxl pip install openpyxl==2.3.5 原文链接:https://stackoverflow.com/questions/38977929/pyinstaller-creating-exe-runti

RecursionError: maximum recursion depth exceeded while calling a Pytho

RecursionError:在调用Python对象时超过最大递归深度 项目 python flask: 在pycharm软件里, default settings --> Build, Execution, Deployment -> Python Debugger. 找到 "Gevent compatible" ,勾选, 打开settings 找到并点击 Build, Execution, Deployment,然后找到并点击Python Debugger,在这个对话款

python递归深度报错--RuntimeError: maximum recursion depth exceeded

当你的程序递归的次数超过999次的时候,就会引发RuntimeError: maximum recursion depth exceeded. 解决方法两个: 1.增加系统的递归调用的次数: import sys sys.setrecursionlimit(n) n为你想要的递归上限 2.优化代码,减少递归的次数. 原文地址:https://www.cnblogs.com/everfight/p/maximum_recursion.html

Python递归报错:RuntimeError: maximum recursion depth exceeded in comparison

Python中默认的最大递归深度是989,当尝试递归第990时便出现递归深度超限的错误: RuntimeError: maximum recursion depth exceeded in comparison 简单方法是使用阶乘重现: 1 #! /usr/bin/env Python 2 3 def factorial(n): 4 5 if n == 0 or n == 1: 6 7 return 1 8 9 else: 10 11 return(n * factorial(n - 1)) >

python --RecursionError: maximum recursion depth exceeded in comparison

在学习汉娜塔的时候,遇到一个error RecursionError: maximum recursion depth exceeded in comparison 经过百度,百度的方法: 加上: import sys sys.setrecursionlimit(100000) 可是我加上之后结果如下,并没有解决问题,python还提示意外退出: 1.再此经过思考(也不是思考,再从头看了学习视频,添加了两个return None,问题解决??????) python 函数要么返回预期的值,要么返

函数递归时,递归次数到900多时,就是抛出异常exception RuntimeError(&#39;maximum recursion depth exceeded&#39;,)

import subprocess import multiprocessing import urllib import sys import os import pymongo import signal import time client=pymongo.MongoClient("192.168.139.143",27017) db=client.domaindb collection=db.domain def getdomain(i): print("proces

win10启动django项目报错 Django RuntimeError: maximum recursion depth exceeded

错误:Django RuntimeError: maximum recursion depth exceeded 原因出自Python\Lib\fuctools.py 把 convert = {    '__lt__': [('__gt__', lambda self, other: other < self),                ('__le__', lambda self, other: not other < self),                ('__ge__', 

pyinstaller打包报错: RecursionError: maximum recursion depth exceeded,UnicodeDecodeError 解决办法

出现原因:     这个错误意思是超过最大递归深度,python默认的递归深度默认是1000),因此当递归深度超过就会引发这样的异常. 解决方法: 1.执行pyinstaller -F XXX.py 它会在你的目录文件生成XXX.spec文件,然后报错,出现该类异常. 2.打开XXX.spec文件,在开头添加上面两行代码. import sys sys.setrecursionlimit(1000000) 3.继续执行打包,但是还文件名:pyinstaller -F XXX.spec ,执行该文