如何从javafx webview解析html并将此数据传输到Jsoup Document? [英] How to parse html from javafx webview and transfer this data to Jsoup Document?

查看:170
本文介绍了如何从javafx webview解析html并将此数据传输到Jsoup Document?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解析某些文档站点的侧边栏TOC(组件表).

I am trying to parse sidebar TOC(Table of Components) of some documentation site.

Jsoup

Jsoup

我尝试过Jsoup.我无法获得TOC元素,因为此标记中的HTML内容不是初始HTML的一部分,而是在页面加载后由JavaScript设置的.

I have tried Jsoup. I can not get TOC elements because the HTML content in this tag is not part of initial HTML but is set by JavaScript after the page is loaded.

您可以在这里看到我之前的问题: JSoup无法在深度2之后解析子元素

You can see my previous question here:JSoup cannot parse child elements after depth 2

建议的解决方案是从浏览器开发工具"菜单中检查手动建立的连接,以找到网站的最新版本.某些文档站点的侧边栏TOC解析只是我的Java程序的一个组件,因此我无法手动执行.

The suggested solution is to examine what connections are made manually from the Browser Dev Tools menu find the last version of the website. Parsing sidebar TOC of some documentation site is just one component of my java program so I cannot do this manually.

JavaFX Webview(不是Android Webview)

JavaFX Webview(not Android Webview)

我尝试使用JavaFX Webview是因为我需要一个能够执行javascript代码并填充Toc标记组件的浏览器.

I have tried JavaFX Webview because I need a browser that executes javascript code and fills Toc tag components.

WebView browser = new WebView();
WebEngine webEngine = browser.getEngine();
webEngine.load("https://docs.microsoft.com/en-us/ef/ef6/");

但是我不知道如何检索已加载网站的HTML代码并将此数据传输到Jsoup Document? 谢谢您的建议.

But I don't know how can I retrieve HTML code of the loaded website and transfer this data to Jsoup Document? ANy advice appreciated.

推荐答案

    WebView browser = new WebView();
    WebEngine webEngine = browser.getEngine();
    String url = "https://docs.microsoft.com/en-us/ef/ef6/";
    webEngine.load(url);
    //get w3c document from webEngine
    org.w3c.dom.Document w3cDocument = webEngine.getDocument();
    // use jsoup helper methods to convert it to string
    String html =  new org.jsoup.helper.W3CDom().asString(webEngine.get);
    // create jsoup document by parsing html
    Document doc = Jsoup.parse(url, html);

这篇关于如何从javafx webview解析html并将此数据传输到Jsoup Document?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆