Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/452.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用JavaFX生成的HTML中的img标记为空_Javascript_Html_Ajax_Javafx_Webengine - Fatal编程技术网

Javascript 使用JavaFX生成的HTML中的img标记为空

Javascript 使用JavaFX生成的HTML中的img标记为空,javascript,html,ajax,javafx,webengine,Javascript,Html,Ajax,Javafx,Webengine,我正在尝试使用JavaFXFrameWeork获取flipkart网站搜索的html。 下面的代码可以很好地为我提供html输出,但有一个小问题。 顺便说一句,我正在使用生成的html浏览flipkart网站。问题 大多数情况下,图像标签中不存在与产品对应的jpeg文件 在html页面中显示产品的名称。我观察到,对于任何产品,前3至6个产品 列出图像标签,否则为空 有标签的如下所示 <DIV class="_3BTv9X" style="height: 240px; width:200px

我正在尝试使用JavaFXFrameWeork获取flipkart网站搜索的html。 下面的代码可以很好地为我提供html输出,但有一个小问题。 顺便说一句,我正在使用生成的html浏览flipkart网站。问题 大多数情况下,图像标签中不存在与产品对应的jpeg文件 在html页面中显示产品的名称。我观察到,对于任何产品,前3至6个产品 列出图像标签,否则为空

有标签的如下所示

<DIV class="_3BTv9X" style="height: 240px; width:200px;">
    <IMG class="_1Nyybr _30XEf0" alt="" src="https://rukminim1.flixcart.com/image/312/312/mobile/d/f/w/motorola-moto-e3-power-pa4c0009in-original-imaemj7xpcfhnu8r.jpeg?q=70"/>
</DIV>
<DIV class="_3BTv9X" style="height: 240px; width: 200px;">
    <IMG class="_1Nyybr" alt=""/>
</DIV>

忘了提及我已经尝试过PhantomJs,但似乎不适用于flipkart.com,因此需要以某种方式使上述java代码正常工作。
public class Main extends Application {

    @Override
    public void start(Stage stage) throws Exception {
    stage.setTitle("HTML");
    stage.setWidth(500);
    stage.setHeight(500);
    //Scene scene = new Scene(new Group());
    //VBox root = new VBox();    
    final WebView browser = new WebView();
    final WebEngine webEngine = browser.getEngine();

    webEngine.load("https://www.flipkart.com/search?q=Motorola&otracker=start&as-show=on&as=off");
    webEngine.getLoadWorker().stateProperty().addListener((observable, oldState, newState) -> {
            if (newState ==  Worker.State.SUCCEEDED) {
                try {
                    ByteArrayOutputStream b = new ByteArrayOutputStream();
                    printDocument(webEngine.getDocument(), b);
                    System.out.println(b.toString());
                    //FlipkartScrape(b.toString());
                    Platform.exit();
                } catch(Exception e) {
                    System.out.println("Caught Exception");
                    Platform.exit();
                }
            }
            });

    //webEngine.load(null);
    //Hyperlink hpl = new Hyperlink("https://www.flipkart.com");

    //root.getChildren().addAll(hpl,browser);
    //scene.setRoot(root);

    //stage.setScene(scene);
    //stage.show();
    }

    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, "yes");
        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);
    }

    public void FlipkartScrape(String html){
    org.jsoup.nodes.Document doc = Jsoup.parse(html);
    Elements a = doc.select("a[title]");
    for (Element next: a) {
        Element e;
        String title = next.attr("title");
        String href = next.attr("href");

        href.replaceAll("/", "\\\\/");
        if ((e = next.nextElementSibling()) != null) {
            e = e.nextElementSibling();
            if (e == null)
                continue;
            e = e.nextElementSibling();
            if (e == null)
                continue;
        } else
            continue;

        if (href.equalsIgnoreCase(e.attr("href"))) {
            href = "http://www.flipkart.com" + href;
            System.out.println(title);
            System.out.println(e.text());
        } else {
            e = e.nextElementSibling();
            if (e == null) continue;

            System.out.println(title);
            href = "http://www.flipkart.com" + href;
            System.out.println("TEXT"+e.text());
        }

        Element parent = next.parent();
        if (parent != null) {
            parent = parent.parent();
            if (parent == null) continue;
        } else {
            continue;
        }

        e = parent.nextElementSibling();
        if (e != null) {
            Elements imgs = e.select("img[class]");
            for (Element img: imgs) {
                String imghref = img.attr("src");
                System.out.println("IMAGEHREF"+imghref);
            }
        }
    }
    }
}