JSoup没有显示所有数据

时间:2018-01-19 10:39:12

标签: java jsoup

这是我的jsoup代码:

Document document = Jsoup.connect("https://www.aliexpress.com/category/200214036/women-watches.html?spm=2114.search0103.3.7.765d221bi3J3Io&site=glo&g=y").get();


Elements titleElement = document.select("div.item > div.img > div.pic > a.picRind > img");

String essay = essayElement.text();

int i = 0;
String linkss[] = new String[titleElement.size()];
for(Element el : titleElement){
    linkss[i] = el.attr("abs:src");
    System.out.println(linkss[i]);
    i++;
}

这是48个结果。

linkss.size()显示48

但是当我打印所有图像src时,它只打印6而其他显示为空白。它需要空间。

这样做的程序是什么。

1 个答案:

答案 0 :(得分:0)

通过打印我发现的所有img元素,前六个img标签的source属性为" src"但其余的" image-src"。为了让所有人都像bekow那样做:

    Document document = Jsoup.connect("https://www.aliexpress.com/category/200214036/women-watches.html?spm=2114.search0103.3.7.765d221bi3J3Io&site=glo&g=y").get();
    Elements titleElement = document.select("div.item > div.img > div.pic > a.picRind > img");
    //print img elements to see if source attribute is "src" or "image-src"
    titleElement.stream().forEach(System.out::println);
    //String essay = essayElement.text();
    int i = 0;
    String linkss[] = new String[titleElement.size()];
    for(Element el : titleElement){
        //check if src value is empty, which is true if the src attr does not exist
        if(!el.attr("abs:src").isEmpty()){
            linkss[i] = el.attr("abs:src");
        }
        else{
            linkss[i] = el.attr("abs:image-src");
        }
        //System.out.println(linkss[i]);
        i++;
    }