简体   繁体   中英

How to read JavaScript output text in a Java Program

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM