extractData method

List<String> extractData({
  1. required String html,
  2. required String selector,
  3. String? attribute,
  4. bool asText = true,
})

Parses HTML content and extracts data using CSS selectors

html is the HTML content to parse selector is the CSS selector to use attribute is the attribute to extract (optional) asText whether to extract the text content (default: true)

Implementation

List<String> extractData({
  required String html,
  required String selector,
  String? attribute,
  bool asText = true,
}) {
  try {
    final document = html_parser.parse(html);
    final elements = document.querySelectorAll(selector);

    return elements.map((element) {
      if (attribute != null) {
        return element.attributes[attribute] ?? '';
      } else if (asText) {
        return element.text.trim();
      } else {
        return element.outerHtml;
      }
    }).toList();
  } catch (e) {
    throw ScrapingException.parsing(
      'Failed to extract data',
      originalException: e,
      isRetryable: false,
    );
  }
}