简体   繁体   中英

JSOUP .attr() method don't extract data from working html

I have an issue with a .attr method which is not working for all attributes besides "class". I've tried to extract the "alt" attribute to get the name of the shop but it just doesn't work. Tried same for an "src" and "data-original" and nothing has been printed out.

Here is the entire method that I've used to extract data.

public List<String> getShops() {
        Elements elements = document.select(".store-logo");
        System.out.println(elements.html());
       for(Element image : elements){
           System.out.println(image.attr("alt"));
       }
        return null;
    }

To make sure that I'm not using the empty document I've printed out entire HTML for all the elements which look like that:

<img src="//image.ceneostatic.pl/imageschain/data/shops_s/20853/logo.jpg;data/custom_images/590/custom_image.png" alt="nalepsze.pl">
<img src="/content/img/icons/pix-empty.png" alt="allegro.pl" data-original="//image.ceneostatic.pl/imageschain/data/shops/20136/logo.jpg;data/custom_images/585/custom_image.png" class="js_lazy">
<img src="/content/img/icons/pix-empty.png" alt="avans.pl" data-original="//image.ceneostatic.pl/imageschain/data/shops/18601/logo.jpg;data/custom_images/585/custom_image.png" class="js_lazy">
<img src="/content/img/icons/pix-empty.png" alt="proshop.pl" data-original="//image.ceneostatic.pl/imageschain/data/shops/29068/logo.jpg;data/custom_images/585/custom_image.png" class="js_lazy">
<img src="/content/img/icons/pix-empty.png" alt="g2a.com" data-original="//image.ceneostatic.pl/imageschain/data/shops/23040/logo.jpg;data/custom_images/585/custom_image.png" class="js_lazy">
<img src="/content/img/icons/pix-empty.png" alt="fotosoft.pl" data-original="//image.ceneostatic.pl/imageschain/data/shops/3914/logo.jpg;data/custom_images/585/custom_image.png" class="js_lazy">
<img src="/content/img/icons/pix-empty.png" alt="techsat24.pl" data-original="//image.ceneostatic.pl/imageschain/data/shops/5666/logo.jpg;data/custom_images/585/custom_image.png" class="js_lazy">
<img src="/content/img/icons/pix-empty.png" alt="imperiumpc.pl" data-original="//image.ceneostatic.pl/imageschain/data/shops/12579/logo.jpg;data/custom_images/585/custom_image.png" class="js_lazy">
<img src="/content/img/icons/pix-empty.png" alt="domsary.eu" data-original="//image.ceneostatic.pl/imageschain/data/shops/4725/logo.jpg;data/custom_images/585/custom_image.png" class="js_lazy">
<img src="/content/img/icons/pix-empty.png" alt="net-s.pl" data-original="//image.ceneostatic.pl/imageschain/data/shops/3653/logo.jpg;data/custom_images/585/custom_image.png" class="js_lazy">
<img src="/content/img/icons/pix-empty.png" alt="sferis.pl" data-original="//image.ceneostatic.pl/imageschain/data/shops/4614/logo.jpg;data/custom_images/585/custom_image.png" class="js_lazy">
<img src="/content/img/icons/pix-empty.png" alt="morele.net" data-original="//image.ceneostatic.pl/imageschain/data/shops/379/logo.jpg;data/custom_images/585/custom_image.png" class="js_lazy">
<img src="/content/img/icons/pix-empty.png" alt="zakupy.vip" data-original="//image.ceneostatic.pl/imageschain/data/shops/29402/logo.jpg;data/custom_images/585/custom_image.png" class="js_lazy">
<img src="/content/img/icons/pix-empty.png" alt="fotoelektro.pl" data-original="//image.ceneostatic.pl/imageschain/data/shops/1671/logo.jpg;data/custom_images/585/custom_image.png" class="js_lazy">
<img src="/content/img/icons/pix-empty.png" alt="3kropki.pl" data-original="//image.ceneostatic.pl/imageschain/data/shops/357/logo.jpg;data/custom_images/585/custom_image.png" class="js_lazy">
<img src="/content/img/icons/pix-empty.png" alt="electro.pl" data-original="//image.ceneostatic.pl/imageschain/data/shops/16202/logo.jpg;data/custom_images/585/custom_image.png" class="js_lazy">
<img src="/content/img/icons/pix-empty.png" alt="allegro.pl" data-original="//image.ceneostatic.pl/imageschain/data/shops/20136/logo.jpg;data/custom_images/585/custom_image.png" class="js_lazy">
<img src="/content/img/icons/pix-empty.png" alt="avans.pl" data-original="//image.ceneostatic.pl/imageschain/data/shops/18601/logo.jpg;data/custom_images/585/custom_image.png" class="js_lazy">
<img src="/content/img/icons/pix-empty.png" alt="proshop.pl" data-original="//image.ceneostatic.pl/imageschain/data/shops/29068/logo.jpg;data/custom_images/585/custom_image.png" class="js_lazy">

Data in this extraction is correct but the next step in for each loop isn't working I get an empty string for every single one of the elements which are quite strange because I can extract a "class" attribute.

I would really appreciate any tips according that topic.

PS. jsoup ver is 1.11.3

In your example you use class "store-logo", but in html document attached none of the img elements has this class. When replacing class name to "js_lazy", your code extracts alt attributes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM