2

我正在尝试使用 JavaFX 框架来获取 Flipkart 网站搜索的 html。下面的代码虽然有一个小故障,但通过让我得到 html 输出效果很好。顺便说一句,我正在使用这个生成的 html 来浏览 Flipkart 网站。问题是html页面中大多数产品的image标签中不存在该产品对应的jpeg文件。我观察到,对于任何产品,前 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>

下面是生成 html 的 Java 程序。我希望有人帮我弄清楚为什么图像标签是空的,以及如何使用正确的 img 标签获取 html 页面。

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);
            }
        }
    }
    }
}
4

0 回答 0