3
我发现BeautifulSoup 4出现逃避一些字符在内嵌的javascript:如何停止BeautifulSoup逃逸联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被转义?或者在输出之前如何使用它?
请注意,它应该是'<! - //',而不是'/ <! - '。虽然对输出没有影响。 – 2013-04-11 10:12:28
''注释在Javascript中实际上是无用的,因为Javascript可以使用'',''''和'>'字符。你应该真正使用'<![CDATA ['和']]>'来恰当地'转义'