51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

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

使用str方法强制转换一个BeautifulSoup对象成字符串的时候报错了,提示是"maximum recursion depth exceeded while calling a Python object",意思大致是"当调用该对象超过最大递归深度"

报错如下所示:

|------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | 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 |

其实原因是:在Python里的递归调用是有限制的,可以使用sys模块里的getrecursionlimit方法查看,代码如下所示:

|-----------|---------------------------------| | 1 | sys.getrecursionlimit() |

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

|---------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | 1 2 3 4 5 6 | Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:37:02) [MSC v.1924 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import sys >>> sys.getrecursionlimit() 1000 >>> |

设置最大深度限制的方法就是setrecursionlimit,至于设置值为多少你可以根据情况设置:

|-----------|-------------------------------------| | 1 | sys.setrecursionlimit(2000) |

深入研究:https://cyrusin.github.io/2015/12/08/python-20151208/


赞(0)
未经允许不得转载:工具盒子 » python最大递归深度错误“maximum recursion depth exceeded while calling a Python object”