[英]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.