0

I want to convert a Webseite loaded by javaFX to JSOUP. p

ublic class database {
    public static ArrayList<String> database = new ArrayList<String>();

    public static ArrayList<Integer> laenge = new ArrayList<Integer>(); 
    public static ArrayList<Integer> tiefe = new ArrayList<Integer>(); 
    public static void main(String[] args) throws IOException, JSONException {
        // TODO Auto-generated method stub


         WebView browser = new WebView();
            WebEngine webEngine = browser.getEngine();
            String url = "http://www.google.com";
            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 htm =  new org.jsoup.helper.W3CDom().asString(webEngine.getDocument());
            // create jsoup document by parsing html
            Document doc = Jsoup.parse(url, htm);




        //Document doc = Jsoup.connect("http://ttp-schreiber.de/Mathematik/index2.html").get();
        Element title = doc.body();
        /*WebView browser = new WebView();
        WebEngine webEngine = browser.getEngine();
        webEngine.load("http://ttp-schreiber.de/Mathematik/index2.html");
        */
        Elements html = doc.select("html");
        Controller(html);
    //  System.out.println("+--+"+deeper(next(html,html.last()))[0][0]);

        databasesafe();
    }

I get these Error Message: Exception in thread "main" java.lang.ExceptionInInitializerError

How is the misttage?

ETC I have the example from here:

How to parse html from javafx webview and transfer this data to Jsoup Document?

In these line ist .get wrong: String html = new org.jsoup.helper.W3CDom().asString(webEngine.get);

1 Answers1

1

This duplicate is probably your best bet. After a page is loaded in WebView, use a Transformer to get its HTML. Then use that HTML string in Jsoup. I am not sure if you will get your expected outcome.

import java.io.File;
import java.nio.file.Files;
import javafx.application.Application;
import javafx.concurrent.Worker;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.jsoup.Jsoup;
import org.w3c.dom.Document;
//import org.jsoup.nodes.Document;


public class App extends Application
{
    org.jsoup.nodes.Document jsoupDocument;

    @Override
    public void start(Stage stage)
    {
        String url = "http://www.google.com";

        WebView webview = new WebView();
    final WebEngine webengine = webview.getEngine();
    webengine.getLoadWorker().stateProperty().addListener((ov, oldState, newState) ->
        {
            if (newState == Worker.State.SUCCEEDED) {
                Document doc = webengine.getDocument();
                try {
                    Transformer transformer = TransformerFactory.newInstance().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");

                    File file = new File("results.txt");
                    file.createNewFile();
                    StreamResult results = new StreamResult(file);
                    transformer.transform(new DOMSource(doc), results);

                    String fileContent = Files.readString(file.toPath());
                    jsoupDocument = Jsoup.parse(fileContent);
                    System.out.println(jsoupDocument.toString());
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });
    webengine.load("http://stackoverflow.com");


        StackPane root = new StackPane(webview);
        stage.setTitle("Hello Drag And Drop");
        Scene scene = new Scene(root, 400, 200);
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args)
    {
        Application.launch(args);
    }   
}
SedJ601
  • 12,173
  • 3
  • 41
  • 59