Beautifulsoup 删除所有内部 javascript [英] beautifulsoup remove all the internal javascript

查看:68
本文介绍了Beautifulsoup 删除所有内部 javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是一个简单的 BeautifulSoup 代码,它有两个内部 JavaScript(不要怪 JavaScript,它只是为了测试目的).

The following is the a simple BeautifulSoup code which has the two internal JavaScript( dont blame about the JavaScript it is just for testing purpose).

from bs4 import BeautifulSoup
html = """
<html><head><title>The Dormouse's story</title>
<script>

var x = 5;
var y = 6;
document.getElementById("demo").innerHTML = x + y;
//document.getElementById("demo").innerHTML = x;
//document.getElementById("demo").innerHTML = y;

</script>
<script>

var x = 5;
var y = 6;
document.getElementById("demo").innerHTML = x + y;
//document.getElementById("demo").innerHTML = x;
//document.getElementById("demo").innerHTML = y;

</script>

</head>
<body>
<p class="title"><b>The Dormouse's story</b></p>

<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>

<p class="story">...</p>
"""
soup = BeautifulSoup(html)
soup.script.decompose()
print soup.prettify()

当我运行这段代码时,它只从文档(Dom 树)中删除了一个

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆