-1

In the following code, the content of HTML is displayed in the console. What I want to do is how can I just show the content of some part of the HTML, for example the HTML content of stock prices?

import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.Scanner;

public class ShowStock {
    public static void main(String[] args) throws IOException {

        String urlString;
        if(args.length == 1)
            urlString = args[0];
        else
        {
            urlString = "https://www.google.com/finance/historical?cid=22144&startdate=Jan+1%2C+2014&enddate=Dec+31%2C+2015&num=30&ei=m-JzVqm2L9fJUaOphsAF";
            System.out.println("Reading data from " + urlString );
        }

        // Open connection
        URL u = new URL(urlString);
        URLConnection connection = u.openConnection();

        // check to make sure the page exists
        HttpURLConnection httpConnection = (HttpURLConnection) connection;
        int code = httpConnection.getResponseCode();
        String message = httpConnection.getResponseMessage();
        System.out.println(code + " " + message);
        if (code != HttpURLConnection.HTTP_OK)
            return;

        // Read server response
        InputStream instream = connection.getInputStream();
        Scanner in = new Scanner(instream);

        // display server response to console
        while (in.hasNextLine())
        {
            String input = in.nextLine();
            System.out.println(input);
        }
    }
}
Nasser
  • 2,118
  • 6
  • 33
  • 56
  • So your question really is "how can I show less data than I'm showing currently?". I'm guessing you didn't write that code either? – Kayaman Dec 18 '15 at 11:33
  • @Kayaman yes this is what I want – Nasser Dec 18 '15 at 11:36
  • 2
    You will have to parse the HTML and identify the bits you want. There are a number of different libraries available for parsing HTML, pick one and give it a shot – ewanc Dec 18 '15 at 11:52

1 Answers1

0

If it is XHTML (html like xml), you can use many xml libraries

If not, use an html parser jsoup, htmlcleaner, ...

see this: Which HTML Parser is the best?

Community
  • 1
  • 1