Java GUI显示网页并返回HTML
我需要如下工作流程:
// load xyz.com in the browser window// the browser is live, meaning users can interact with it
browser.load("http://www.google.com");
// return the HTML of the initially loaded page
String page = browser.getHTML();
// after some time
// user might have navigated to a new page, get HTML again
String newpage = browser.getHTML();
令我惊讶的是,这与Java GUI(例如JavaFX(http://lexandera.com/2009/01/extracting-html-from-
a-webview/))和Swing 有多么困难。
有一些简单的方法可以在Java中获得此功能吗?
回答:
这是一个使用JavaFX的人为示例,该示例将html内容打印到System.out
-适应创建getHtml()
方法应该不会太复杂。(我已经用JavaFX 8对其进行了测试,但是它也应该可以与JavaFX 2一起使用)。
每次加载新页面时,该代码都会打印HTML内容。
注意:我已printDocument
从此答案中借用了代码。
public class TestFX extends Application { @Override
public void start(Stage stage) throws Exception {
try {
final WebView webView = new WebView();
final WebEngine webEngine = webView.getEngine();
Scene scene = new Scene(webView);
stage.setScene(scene);
stage.setWidth(1200);
stage.setHeight(600);
stage.show();
webEngine.getLoadWorker().stateProperty().addListener(new ChangeListener<Worker.State>() {
@Override
public void changed(ObservableValue<? extends State> ov, State t, State t1) {
if (t1 == Worker.State.SUCCEEDED) {
try {
printDocument(webEngine.getDocument(), System.out);
} catch (Exception e) { e.printStackTrace(); }
}
}
});
webView.getEngine().load("http://www.google.com");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void printDocument(Document doc, OutputStream out) throws IOException, TransformerException {
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
transformer.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(out, "UTF-8")));
}
public static void main(String[] args) {
launch(args);
}
}
以上是 Java GUI显示网页并返回HTML 的全部内容, 来源链接: utcz.com/qa/403647.html