Home > Java > javaTutorial > body text

Java Example - Web Scraping

黄舟
Release: 2017-01-20 11:58:43
Original
1318 people have browsed it

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();
   }}
Copy after login

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"/>……
Copy after login

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)!


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!