gpt4 book ai didi

python - 运行时错误 : maximum recursion depth exceeded when parsing HTML

转载 作者:太空宇宙 更新时间:2023-11-04 10:01:55 25 4
gpt4 key购买 nike

我有一个测试程序,我试图从每个 HTML 标记中提取所有文本。在我重写 __init__ 函数之前,它似乎一直有效。

我得到错误:

 File "./html.py", line 9, in __init__
TransParser.__init__(self)
File "./html.py", line 9, in __init__
TransParser.__init__(self)
File "./html.py", line 9, in __init__
TransParser.__init__(self)
File "./html.py", line 9, in __init__
TransParser.__init__(self)
RuntimeError: maximum recursion depth exceeded

程序在这里:

from HTMLParser import HTMLParser

trans = {
'History': 'History TRANSLATED',
'Acknowledgements': 'Acknowledgements TRANSLATED'
}

inputHTML = """<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td class="section-title"><a href="/about%20/history">History</a> </td>

<td class="section-title"><a href="/about/team">Project Team</a></td>

<td class="section-title"><a href="/about/data">Contributors of data</a></td>

<td class="section-title"><a href=
"/about/acknowledgements">Acknowledgements</a></td>

<td class="section-title"><a href="/about/origins">African Origins
Project</a></td>

<td class="section-title"><a href="/about/contacts">Contact us</a></td>
</tr>
</tbody>
</table>

<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td class="section-desc">A brief account of the origins of a single multi-source
dataset of the trans-Atlantic slave trade and its realization first as a CD-ROM
published by Cambridge University Press in 1999 and now, in an expanded version,
on the Voyages website.</td>

<td class="section-desc">Names of the principal investigators, of members of the
project development team, and of individuals serving on the steering committee
and advisory board.</td>

<td class="section-desc">Names of scholars and researchers whose findings on the
trans-Atlantic slave trade have been incorporated into the Voyages Database.</td>

<td class="section-desc">Major sponsors and institutional partners of the Voyages
website, as well as other organizations and individuals who have assisted the
work of the project team.</td>

<td class="section-desc">A scholar-public collaborative project using audio
recordings of names in African Names Database to trace the geographic origins of
Africans transported in the transatlantic slave trade.</td>

<td class="section-desc">Members of the Voyages editorial board and the email
address for contacting the website.</td>
</tr>
</tbody>
</table>"""


class TransParser(HTMLParser):
def __init__(self):
TransParser.__init__(self)
self.trans_data = self.rawdata

def handle_data(self, data):
data = data.strip()
if data:
section = trans.get(data, data)
#self.trans_data = self.trans_data.replace(data, section)

parser = TransParser()
parser.feed(inputHTML)

最佳答案

class TransParser(HTMLParser):
def __init__(self):
TransParser.__init__(self)
# ^^^ You are calling the same constructor recursively.

修复:

class TransParser(HTMLParser):
def __init__(self):
HTMLParser.__init__(self)
..
..

关于python - 运行时错误 : maximum recursion depth exceeded when parsing HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43129757/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com