Get Javascript tag content
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)
精彩评论