在不使用任何外部库的情况下,将网站的 HTML 内容提取为字符串的最简单方法是什么?
最佳答案
我目前正在使用这个:
String content = null;
URLConnection connection = null;
try {
connection = new URL("http://www.google.com").openConnection();
Scanner scanner = new Scanner(connection.getInputStream());
scanner.useDelimiter("\\Z");
content = scanner.next();
scanner.close();
}catch ( Exception ex ) {
ex.printStackTrace();
}
System.out.println(content);
但不确定是否有更好的方法。
关于java - 如何在 Java 中获取 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31462/