I have discovered that BeautifulSoup 4 appears to escape some characters in inline 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>
In case it's lost in the above, the key problem is that:
if (4 > 3 && 3 < 4)
gets converted into:
if (4 > 3 && 3 < 4)
which doesn't work particularly well ...
I have tried the included formatters in the prettify()
method, with no success.
So any idea how to stop the javascript being escaped? Or how to unescape it before outputting it?