这是我的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,其他显示为空白。虽然需要空格。

这样做的程序是什么。

最佳答案

通过打印所有img元素,我刚发现前六个img标签的source属性是“ src”,而其余的“ image-src”。为了使所有人都喜欢做以下事情:

    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++;
    }

08-25 14:14
查看更多