extractData method
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('Failed to extract data: $e');
}
}