如何在Java中获取HTML
时间:2020-03-05 18:44:10 来源:igfitidea点击:
在不使用任何外部库的情况下,将网站的HTML内容提取为String的最简单方法是什么?
解决方案
回答
我目前正在使用此:
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(); }catch ( Exception ex ) { ex.printStackTrace(); } System.out.println(content);
但不确定是否有更好的方法。
回答
我只是将这个帖子留在了其他主题中,尽管我们上面的内容也可能会起作用。我认为任何一个都不比另一个容易。只需使用代码顶部的import org.apache.commons.HttpClient即可访问Apache软件包。
编辑:忘记了链接;)
回答
这对我来说效果很好:
URL url = new URL(theURL); InputStream is = url.openStream(); int ptr = 0; StringBuffer buffer = new StringBuffer(); while ((ptr = is.read()) != -1) { buffer.append((char)ptr); }
不知道所提供的其他解决方案是否更有效率。