Search code examples
flutterdartweb-scrapinggetelementsbyclassname

How to scrape "style" attribute value in Flutter/Dart


I'm trying to scrape data from a webpage. I wanted to get the URL present inside the style attribute(as marked in picture)

enter image description here

So far I tried his but it returns me empty strings

import 'dart:convert';
import 'package:html/dom.dart' as dom;
import 'package:html/parser.dart' as parser;
import 'package:http/http.dart' as http;


Future initiate() async {
  final response = await http.get('https://pixxelprecision.co.uk/portfolio');
  dom.Document document = parser.parse(response.body);

  final imgUrl = document.getElementsByClassName('w-ibanner-image');
  imgUrls = imgUrl.map((element) => element.attributes['style']).toList();

  print('URLs:  $imgUrls');

}

Console: enter image description here


Solution

  • style attribute initially has no value. Value is set by JS while page rendering. You can try to scrape @data-bg instead:

    imgUrls = imgUrl.map((element) => element.attributes['data-bg']).toList();