我发现 BeautifulSoup 4 似乎转义了内联 javascript 中的一些字符:
>>> print s
<DOCTYPE html>
<html>
<body>
<h1>Test page</h1>
<script type="text/javascript">
//<!--
if (4 > 3 && 3 < 4) {
console.log("js working");
}
//-->
</script>
</body>
</html>
>>> import bs4
>>> soup = bs4.BeautifulSoup(s, 'html5lib')
>>> print soup
<html><head></head><body><doctype html="">
<h1>Test page</h1>
<script type="text/javascript">
//<!--
if (4 > 3 && 3 < 4) {
console.log("js working");
}
//-->
</script>
</doctype></body></html>
>>> print soup.prettify()
<html>
<head>
</head>
<body>
<doctype html="">
<h1>
Test page
</h1>
<script type="text/javascript">
//<!--
if (4 > 3 && 3 < 4) {
console.log("js working");
}
//-->
</script>
</doctype>
</body>
</html>
万一它在上面丢失了,关键问题是:
if (4 > 3 && 3 < 4)
转换为:
if (4 > 3 && 3 < 4)
这不是特别好用...
我已经尝试了该prettify()
方法中包含的格式化程序,但没有成功。
那么知道如何阻止javascript被转义吗?或者如何在输出之前取消它?