【发布时间】:2015-02-20 00:52:29
【问题描述】:
您好,我正在尝试阅读 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
【问题讨论】:
标签: java javascript html