获取网页的源码:

package com.atguigu.crud.controller;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

public class ssssss {
      public static void main(String[] a){
            try {
                URL url=new URL("http://www.baidu.com");
                BufferedReader reader=new BufferedReader(new InputStreamReader(url.openStream()));
                BufferedWriter writer=new BufferedWriter(new FileWriter("index.html"));
                String line;
                while((line=reader.readLine())!=null){
                    System.out.println(line);
                    writer.write(line);
                    writer.newLine();
                }
                reader.close();
                writer.close();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
}

 还有一个更简单的: 需要用到 jsoup-1.7.2.jar

String html = Jsoup.connect("你的网址").get().html();

 

版权声明:本文为suiyisuixing原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/suiyisuixing/p/7616805.html