Java爬虫入門(一)


package   ;

import java.io.*;
import java.net.*;

public class pachong1 {
    public static void main(String[] args) {
        //         
        String strurl = "https://www.w3cschool.cn/java/java-tutorial.html";
        try{
            //     url
            URL url = new URL(strurl);
            //  url          
            URLConnection com = url.openConnection();
            //       ,            
            InputStream word = com.getInputStream();
            System.out.println(com.getContentEncoding());

            //         ,       
            //  BufferedReader InputStreamReader              (=。=     )
            //      gbk  utf8
            BufferedReader br = new BufferedReader(new InputStreamReader(word,"utf8"));

            //    String       
            String line = null;

            //         
            while ((line = br.readLine()) != null){
                System.out.println(line);
            }
            //    
            br.close();

        }catch (Exception e){
            e.printStackTrace();
        }
    }

}