【发布时间】:2021-04-27 22:11:56
【问题描述】:
BS4 更正了错误的 html。通常这不是问题。我尝试解析、更改和保存此页面的 html:ulisses-regelwiki.de/index.php/sonderfertigkeiten.html 在这种情况下,修复会更改表示。修复后页面多行不再居中,而是左对齐。 由于我必须处理所述页面的损坏的 html,我不能简单地修复 html 代码。
如何防止 bs4 修复 html 或以某种方式修复“更正”?
(这个最小的例子只是显示 bs4 修复损坏的 html 代码;我无法创建一个最小的例子,其中 bs4 以错误的方式执行此操作,就像上面提到的页面一样)
#!/usr/bin/env python3
from bs4 import BeautifulSoup
html = '''
<!DOCTYPE html>
<center>
Some Test content
<!-- A comment -->
<center>
'''
def is_string_only(t):
return type(t) is NavigableString
soup = BeautifulSoup(html, 'lxml') #or html.parse
print(str(soup))
【问题讨论】:
标签: python html parsing web-scraping beautifulsoup