Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 美丽集团`<;br>;`来自输入的标记处理_Python_Html_Beautifulsoup - Fatal编程技术网

Python 美丽集团`<;br>;`来自输入的标记处理

Python 美丽集团`<;br>;`来自输入的标记处理,python,html,beautifulsoup,Python,Html,Beautifulsoup,在我的html中有很多标记(未关闭)。测试html如下所示: html = r"<html><head><title>s</title></head><body>something <br> <b> another thing</b> " \ r"<br> even more <br> <b> end</b></body&

在我的html中有很多

标记(未关闭)。测试html如下所示:

html = r"<html><head><title>s</title></head><body>something <br> <b> another thing</b> " \
       r"<br> even more <br> <b> end</b></body></html>"
html=r“s方法
另一件事”\ r“
甚至更多
结束”
这是一个正确的html用法,对吗?简单且“过时”,但仍然合法

现在我注意到beatifulsoup将这个html转换成了一团乱麻,它不再是一个“在
下面只有一层深度的平面树”,而是变成了许多层,使得分析变得很麻烦。显示html还显示以下内容:

html = r"<html><head><title>s</title></head><body>something <br> <b> another thing</b> " \
       r"<br> even more <br> <b> end</b></body></html>"
soup = BeautifulSoup(html, "html.parser")
print(soup)
html=r“s方法
另一件事”\ r“
甚至更多
结束” soup=BeautifulSoup(html,“html.parser”) 印花(汤)
输出:
某物
另一物
更多的
结束


如果我使用“html5lib”,这种行为会发生变化,此时输出会变成(我实际上希望得到):

输出:
某物
另一物
甚至更多
结束


现在,虽然html5lib“修复”了这个问题,但这仍然不是我所希望的:我正在提供合法的
html
代码,对吗?那么为什么不同的解析器会给出不同的结果呢?

您可以检查解析器之间的差异

html5lib
试图通过像浏览器一样读取文档来修复文档格式无效的问题,如果您想打印格式良好的HTML文档,这是最好的选择

lxml
做同样的事情,但速度更快。很多

默认解析器不适用于此目的,如果您想使用它进行简单的操作,它可以正常工作,如果您想维护文档的结构,请使用上面提到的解析器

如英国标准协会所述:

HTML解析器之间也存在差异。如果给Beauty Soup一个格式完美的HTML文档,这些差异就无关紧要了。一个解析器会比另一个解析器更快,但它们都会提供与原始HTML文档完全相同的数据结构。 但是,如果文档的格式不完美,不同的解析器将给出不同的结果


除了“doctype声明”之外,w3的验证器认为上面的HTML行格式正确:但差异确实很重要。对于默认解析器,


不同。当打印文档时,他会尝试关闭它,因为他认为标记无效。尽管如此,你是对的,文档是有效的,html.parser不会以同样的方式“思考”,这就是为什么在这种情况下,
lxml
html5lib
是更好的选择。这已被(你?)报告为BeautifulSoup中的一个bug: