Home  >  Article  >  Java  >  Java Example - Web Scraping

Java Example - Web Scraping

黄舟
黄舟Original
2017-01-20 11:58:431386browse

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 (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn