[英]How to read JavaScript output text in a Java Program
嗨,我正在尝试在Java程序中阅读forbes.com关于当天的想法。 但是在网页的查看源中,我没有在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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.