使用URL读取网页内容,通过URL对象的openStream()方法可以得到指定资源的输入流,通过流能够读取或访问网页上的资源
代码:
package kun; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.URL; public class mainfun { public static void main(String[] arg)throws Exception { URL url = new URL("https://www.taobao.com/"); //需要指定编码方式,防止出现中文乱码 BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream(),"UTF-8")); String inputLine; while ((inputLine = in.readLine()) != null) { System.out.println(inputLine); } in.close(); } }
保存下来的内容是html文档: