The following example demonstrates how to use the URL() constructor of the net.URL class to crawl a web page:
/* author by w3cschool.cc Main.java */import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.FileWriter;import java.io.InputStreamReader;import java.net.URL;public class Main { public static void main(String[] args) throws Exception { URL url = new URL("http://www.w3cschool.cc"); BufferedReader reader = new BufferedReader (new InputStreamReader(url.openStream())); BufferedWriter writer = new BufferedWriter (new FileWriter("data.html")); String line; while ((line = reader.readLine()) != null) { System.out.println(line); writer.write(line); writer.newLine(); } reader.close(); writer.close(); }}
The output result of running the above code is (the source code of the web page, stored in the current directory data.html file under):
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"/> <meta http-equiv="X-UA-Compatible" content="IE=11,IE=10,IE=9,IE=8"/>……
The above is the Java example-web page crawling content. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!