如何在Java程序中读取JavaScript输出文本

时间:2015-02-19 13:03:10

标签: java javascript html

您好我正在尝试阅读forbes.com关于Java程序当天的想法。但在网页的视图源中我没有得到在html页面上呈现的输出。关于如何读取渲染输出的任何线索?

下面是我正在阅读思想的网站的源代码。

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

我已经最小化并消除了所有混乱,并尽可能保持基本。

这是网站的观看源

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

这是我的Java程序

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

}

这是程序输出

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

[溶液]

经过meister_reineke的一些帮助后,继承了有效的java代码:)并解决了这个问题。

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

}

上述代码的输出是

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

1 个答案:

答案 0 :(得分:1)

您可以在这里找到答案:

Getting Final HTML with Javascript rendered Java as String

这看起来有点像你的问题,也许HtmlUnit也可以帮助你。