开发者

Get Javascript tag content

开发者 https://www.devze.com 2022-12-14 13:16 出处:网络
Hi I want to save a website\'s source code into a file using java. From the source code i want to get only <script> </scrip开发者_如何学JAVAt> tag contents how can i do that?Use an HTML pa

Hi I want to save a website's source code into a file using java. From the source code i want to get only <script> </scrip开发者_如何学JAVAt> tag contents how can i do that?


Use an HTML parser in Java to extract text from HTML.


Once you've loaded the source code to a variable in Java, find the position of <script> and the position of </script> in the file and delete everything that's not inside that range.

Something like:

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

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

So the substring would be:

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

(This may be plainly wrong, I can't test it at the moment, sorry)

0

精彩评论

暂无评论...
验证码 换一张
取 消