求助:网页JS脚本过滤问题

2013-05-11 20:27:48 +08:00
 loginv2
<html>
<head>
<script></script>
</head>
<body>
html内容html内容html内容html内容html内容
<script></script>
html内容html内容html内容
<div>
<script>1</script>html内容html内容html内容
</div>
<span>
<script>2</script>
</span>

<xxx><script>3</script></xxx>


</body>
<script></script>
</html>

如何过滤上述代码的 script标签及内容
条件1 保持html的结构
条件2 不能

方法不限 求思路
2686 次点击
所在节点    问与答
6 条回复
funagi
2013-05-11 20:42:41 +08:00
条件2 不能 [什么?]

一般都是用正则替换掉<script>.+?</script>

好吧,其实我自己都觉得没有完全理解楼主的问题
loginv2
2013-05-11 20:52:31 +08:00
条件2 不能误伤1 2 3 这三处标签
crazybubble
2013-05-11 21:37:09 +08:00
用BeautifulSoup的话,先选中head,再在head里选中script,然后extract掉
loginv2
2013-05-11 22:00:29 +08:00
那body里面的怎么办

我的大概意思就是 浏览器是怎么识别html中 脚本和 文本的区别的?

脚本用于了执行
而文本用来显示 不执行
funagi
2013-05-11 22:40:02 +08:00
@loginv2
文本形式的<script>在HTML中的正确表示法应该是&lt;script&gt;
http://www.ostools.net/commons?type=2
loginv2
2013-05-12 00:32:54 +08:00
这只是一种表示法
不转义的话 也可以显示的,当这种情况出现的时候就不知道怎么判断了

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/68511

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX