0

Hi i am trying to read forbes.com 's thought of the day in my Java program. But in the view source of the webpage i am not getting the output rendered on the html page. Any clues as to how to read the rendered output?

Heres the source code of website where i am reading the thoughts from.

<head>
    <script src="http://images.forbes.com/scripts/dart_forbes.js"></script>
    <script src="http://images.forbes.com/welcome/desktop/welcome_js.js?v=1.5"></script>
</head>
<body>
    <script language="JavaScript">
        forbes_dart.ad('thoughtx', '600x100');
    </script>
</body>

I have minimised and removed all clutter and kept it as basic as possible.

Heres the view source of the site

<html>
    <head>
        <script src="./js/dart_forbes.js"></script>
        <script src="./js/welcome_js.js"></script>
    </head>
    <body>
        <script language="JavaScript">
            forbes_dart.ad('thoughtx', '600x100');
        </script>
    </body>
</html>

Here's my Java program

public class extractor {

    public static void main(String args[]) throws Exception{
        extractor t = new extractor();
        t.connect();
    }

    public void connect() throws Exception {
        URL obj = new URL("http://localhost:8080/q2p/thought.html");
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();

        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();

        System.out.println(response.toString());
    }

}

Here's the program output

<html>    <head>        <script src="./js/dart_forbes.js"></script>        <script src="./js/welcome_js.js"></script>    </head>    <body>        <script language="JavaScript">            forbes_dart.ad('thoughtx', '600x100');        </script>    </body></html>

[SOLUTION]

Well after some help from meister_reineke heres the java code which works :) and solves the problem.

public class extractor {

    public static void main(String args[]) throws Exception{
        extractor t = new extractor();
        t.connect();
    }

    public void connect() throws Exception {
        URL obj = new URL("http://localhost:8080/q2p/thought.html");

        WebClient webClient = new WebClient(BrowserVersion.CHROME);
        HtmlPage myPage = ((HtmlPage) webClient.getPage(obj));

        System.out.println(myPage.asText());
        webClient.closeAllWindows();
    }

}

The output for the above code is

Patience strengthens the spirit, sweetens the temper, stifles anger, extinguishes envy, subdues pride, bridles the tongue.
Share
Facebook Twitter LinkedIn Google
George Horne

1 Answer 1

2

Here you might find the answers:

Getting Final HTML with Javascript rendered Java as String

this looks a bit similiar to your question, maybe HtmlUnit can help you as well.

Sign up to request clarification or add additional context in comments.

1 Comment

trying this out will let u know. Thanks :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.