如何在Java中获取HTML [英] How to fetch HTML in Java

查看:83
本文介绍了如何在Java中获取HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果没有使用任何外部库,将网站的HTML内容获取到字符串中的最简单方法是什么?

>我正在使用这个:

  String content = null; 
URLConnection连接= null;
尝试{
connection = new URL(http://www.google.com).openConnection();
扫描仪扫描仪=新扫描仪(connection.getInputStream());
scanner.useDelimiter(\\Z);
content = scanner.next();
} catch(Exception ex){
ex.printStackTrace();
}
System.out.println(content);

但不确定是否有更好的方法。


Without the use of any external library, what is the simplest way to fetch a website's HTML content into a String?

解决方案

I'm currently using this:

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);

But not sure if there's a better way.

这篇关于如何在Java中获取HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆