【发布时间】:2014-10-04 17:20:02
【问题描述】:
如果有以下 html 块:
chunk = '<p>BLA bla bla html... <div>Copyright 2014 NPR</div></p>'
当我执行以下操作时:
from bs4 import BeautifulSoup
soup = BeautifulSoup(chunk)
块变成这样:
>>> soup
<html><body><p>BLA bla bla html... </p><div>Copyright 2014 Someone</div></body></html>
段落标签被提前关闭,并且 div 被拉出它。
我对此感到惊讶。这是 BeautifulSoup 的预期行为吗?如果是这样,谁能解释它为什么这样做?
编辑: 请注意,我意识到这个 html 是无效的,但我没有意识到 BeautifulSoup 会在这种程度上编辑无效的 html。 Here's a related SO question on invalid HTML (div instead a p tag)
【问题讨论】:
标签: python html python-3.x beautifulsoup html-parsing