这是一个beautifulsoup过程,可获取所有<p> html标签内的内容。从某些网页上获取内容后,出现错误,提示已超过最大递归深度。

def printText(tags):
    for tag in tags:
        if tag.__class__ == NavigableString:
            print tag,
        else:
            printText(tag)
    print ""
#loop over urls, send soup to printText procedure

跟踪的底部:
 File "web_content.py", line 16, in printText
    printText(tag)
  File "web_content.py", line 16, in printText
    printText(tag)
  File "web_content.py", line 16, in printText
    printText(tag)
  File "web_content.py", line 16, in printText
    printText(tag)
  File "web_content.py", line 16, in printText
    printText(tag)
  File "web_content.py", line 13, in printText
    if tag.__class__ == NavigableString:
RuntimeError: maximum recursion depth exceeded in cmp

最佳答案

您可能打了一个琴弦。遍历字符串会产生1个长度的字符串。遍历该1长度的字符串将产生一个1长度的字符串。遍历1个长度的字符串...

09-19 00:33