假设我们有一个html
文件,如下所示:
test.html
<div>
<i>Some text here.</i>
Some text here also.<br>
2 + 4 = 6<br>
2 < 4 = True
</div>
如果我将此html
传递给BeautifulSoup
,它将逃避&
实体附近的plus
符号,并且输出html
将是这样的:
<div>
<i>Some text here.</i>
Some text here also.<br>
2 &plus 4 = 6<br>
2 < 4 = True
</div>
示例python3
代码:
from bs4 import BeautifulSoup
with open('test.html', 'rb') as file:
soup = BeautifulSoup(file, 'html.parser')
print(soup)
如何避免这种行为?
答案 0 :(得分:3)
阅读不同解析器库的描述:https://www.crummy.com/software/BeautifulSoup/bs4/doc/#installing-a-parser:
这可以解决您的问题:
s = '''
<div>
<i>Some text here.</i>
Some text here also.<br>
2 + 4 = 6<br>
2 < 4 = True
</div>'''
soup = BeautifulSoup(s, 'html5lib')
您会得到:
>>> soup
<html><head></head><body><div>
<i>Some text here.</i>
Some text here also.<br/>
2 + 4 = 6<br/>
2 < 4 = True
</div></body></html>