获取Javascript标记内容

时间:2009-12-23 01:56:23

标签: java javascript

您好我想使用java将网站的源代码保存到文件中。从源代码我只想获得<script> </script>标签内容我该怎么做?

2 个答案:

答案 0 :(得分:3)

使用Java中的HTML解析器从HTML中提取文本。

答案 1 :(得分:1)

将源代码加载到Java中的变量后,在文件中找到<script>的位置和</script>的位置,并删除不在该范围内的所有内容。

类似的东西:

String sourceCode  = "source code here"
String startTag    = "<script>";
String endTag      = "</script>";

    int startInt = sourceCode.indexOf(startTag);
    int endInt   = sourceCode.indexOf(endTag);

所以子串将是:

String jsCode   = sourceCode.substring(startInt,endInt);

(这可能是明显错误的,我现在无法测试,对不起)