简体   繁体   English

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

[英]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. 嗨,我正在尝试在Java程序中阅读forbes.com关于当天的想法。 But in the view source of the webpage i am not getting the output rendered on the html page. 但是在网页的查看源中,我没有在html页面上呈现输出。 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 这是我的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());
    }

}

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

}

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 用Javascript获取最终HTML将Java渲染为字符串

this looks a bit similiar to your question, maybe HtmlUnit can help you as well. 这看起来有点类似于您的问题,也许HtmlUnit也可以为您提供帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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