Parsing text from a website in Java

A simple java class can be written in order to parse some text from a random website.

As a starting point we may use a simple code below that would allow us to store the parsed results as a text file. There are two methods only within the class below, one for making connection and another one for parsing the text:

/* Parses raw text from the web address given and stores it in a text file.
 * D.Gasior @ algoPositive.com, 2017 
 */



import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.io.*;


public class connect {
  String address = null;
  public connect(String address){
    this.address=address;
  }
  
  
//Make a connection method
  public void getURL(){
  try{
  URL newURL= new URL(address);
  URLConnection fromURLconnection = newURL.openConnection();
    fromURLconnection.connect();
    readText(newURL);
  }
  catch (MalformedURLException e) { 
      System.out.println("Try again");

      
  } 
  catch (IOException e) {   
    System.out.println("Connection problem-Try again:");
    

      }
  }
    
//Parse and store text method
  public void readText(URL u){
    
     try {
      BufferedReader in = new BufferedReader(new InputStreamReader(u.openStream()));
      BufferedWriter writer= new BufferedWriter(new FileWriter("writeFile.txt")); 
      String inputLine;
    
          while ((inputLine = in.readLine()) != null){ 	
          writer.write(inputLine);
          
          }
          
          in.close();
          writer.close();
          } 
     catch (IOException e) {
      
      e.printStackTrace();
      System.out.println("Error reading the page,sorry.");
    }
    
  }	
  
}

Main class:

/*Simple Main class
 * Author: D.Gasior @ algoPositive.com, 2017  
 */
public class Fetch {
    
  public static void main(String [] args){
    
    new Fetch();
    connect newConnection=new connect("https://stackoverflow.com/questions/46402922/"
        + "how-can-i-get-an-event-for-if-the-camera-has-moved-in-aframe");
    newConnection.getURL();
    
  }

}

 

 

 

4 Comments on Parsing text from a website in Java

  1. Wonderful blog! I found it while Ьrowsing on Yahoo News. Do you have any sսggestions ⲟn how to get listeɗ in Yahoo News?
    I’ve been trying for a while but I never seem to get theгe!
    Appreciate it

  2. Aһaa, its fastidious discusѕion on the topic of
    this piece of writing at this place at this web site, I have read
    аll that, so now mе also commenting here.

  3. I’m amazed, I must say. Seldom do I encounter a blog that’s both educative and amusing, and let me tell you, you have hit
    the nail on the head. The issue is something not enough folks are speaking intelligently
    about. I’m very happy I found this in my hunt
    for something relating to this.