JAVA - 基本爬文解析
public String getJSON(String url) {
System.out.println(url);
String content = null;
URLConnection connection = null;
try {
connection = new URL(url).openConnection();
Scanner scanner = new Scanner(connection.getInputStream());
scanner.useDelimiter("\\Z");
content = scanner.next();
Pattern pattern = Pattern.compile("<pre.*?>.*?</pre>",Pattern.CASE_INSENSITIVE|Pattern.DOTALL);
Matcher matcher = pattern.matcher(content);
if(matcher.find()) {
content = matcher.group();
}
content = content.replaceAll(""", "\"");
content = content.replaceFirst("<pre.*?>", "");
content = content.replaceFirst("</pre>.*?", "");
}catch ( Exception ex ) {
ex.printStackTrace();
}
System.out.println(content);
return content;
}