extractData method
Extracts data from the page using CSS selectors
Implementation
Future<Map<String, dynamic>> extractData(
Map<String, String> selectors, {
Map<String, String>? attributes,
}) async {
_checkDisposed();
if (_controller == null) {
throw ScrapingException.validation(
'Browser not initialized',
isRetryable: false,
);
}
final result = <String, dynamic>{};
for (final entry in selectors.entries) {
final key = entry.key;
final selector = entry.value;
final attribute = attributes?[key];
try {
String script;
if (attribute != null) {
script = """
(function() {
const elements = document.querySelectorAll('$selector');
if (elements.length === 0) return null;
if (elements.length === 1) {
return elements[0].getAttribute('$attribute');
} else {
return Array.from(elements).map(el => el.getAttribute('$attribute'));
}
})();
""";
} else {
script = """
(function() {
const elements = document.querySelectorAll('$selector');
if (elements.length === 0) return null;
if (elements.length === 1) {
return elements[0].textContent.trim();
} else {
return Array.from(elements).map(el => el.textContent.trim());
}
})();
""";
}
final value = await _controller!.evaluateJavascript(source: script);
result[key] = value;
} catch (e) {
_log('Error extracting data for selector $selector: $e', isError: true);
result[key] = null;
}
}
return result;
}