flutter-html

Confused about html parsing


I am trying to get images from a online book website.This is what I have done so far

 var listAll= document
        .querySelectorAll("div.prd-wrapper div.product-img")
        .map((e) => e.getElementsByTagName("a")[0])
        .toList();
    listAll.forEach((element) {
     print(element.innerHtml);
   });
  }

This code give this output

  <img class="lazyload" data-src="https://i.dr.com.tr/cache/154x170-0/originals/0001742911001-1.jpg" alt="Minik Elif'in Tuvalet 

 <img class="lazyload" data-src="https://i.dr.com.tr/cache/154x170-0/originals/0001847750001-1.jpg" alt="Unutma Mutlu Bir Hayat Çok Az 

I am required to get image links.I tried this.But Doesnt work

  var img=listAll.forEach((element) {
      print(element.getElementsByTagName("data-src").length);
    });

Solution

  • Concerning this line:

    element.getElementsByTagName("data-src")

    data-src is an attribute, not a tag, which is probably why getElementsByTagName does not return a useful result.

    You would want use something like getAttributeByName (or whatever your method your lib provides).