我有以下代码从 html 文件中删除重复的段落。
from bs4 import BeautifulSoup
fp = open("Input.html", "rb")
soup = BeautifulSoup(fp, "html5lib")
elms = []
for elem in soup.find_all('font'):
if elem not in elms:
elms.append(elem)
else:
target =elem.findParent().findParent()
target.decompose()
print(soup.html)
几乎可以工作,但是对于某些元素,我收到此错误
attributeerror: 'nonetype' object has no attribute 'findparent'
有没有办法在发生错误的 HTML 文件中打印行号以检查格式是什么?
代码没有问题的元素结构是这样的
<!DOCTYPE html>
<html>
<body>
<p align="left">
<b><font face="Times New Roman" size="5" color="red">Some text</font></b>
</p>
</body>
</html>
但是由于文件有点大,我没有确定代码卡住的元素的结构。