extract_table method
Future<Online>
extract_table(
- AbstractSelector selector,
- String key, {
- String headerSelector = 'th',
- String rowSelector = 'tr',
- String cellSelector = 'td',
Extracts data from a table and stores it in the context.
selector
is the AbstractSelector to find the table.
key
is the key under which the extracted data will be stored in the context.
headerSelector
(optional) specifies a custom selector for table headers.
rowSelector
(optional) specifies a custom selector for table rows.
cellSelector
(optional) specifies a custom selector for table cells.
Returns the Online instance for method chaining.
Example usage:
await online
.extract_table(Css('table.data-table'), 'tableData')
.extract_table(Css('div.custom-table'), 'customTableData',
headerSelector: '.header',
rowSelector: '.row',
cellSelector: '.cell'
);
var tableData = ctx.get<List<Map<String, String>>>('tableData');
var customTableData = ctx.get<List<Map<String, String>>>('customTableData');
Implementation
Future<Online> extract_table(AbstractSelector selector, String key,
{String headerSelector = 'th',
String rowSelector = 'tr',
String cellSelector = 'td'}) async {
Show.action('extracting', 'table data', 'from', selector.selector);
await (await page).waitForSelector(selector.selector);
var tableData = await (await page).evaluate('''
(selector, headerSelector, rowSelector, cellSelector) => {
const table = document.querySelector(selector);
const headers = Array.from(table.querySelectorAll(headerSelector)).map(th => th.textContent.trim());
const rows = Array.from(table.querySelectorAll(rowSelector)).slice(1);
return rows.map(row => {
const cells = Array.from(row.querySelectorAll(cellSelector)).map(td => td.textContent.trim());
return Object.fromEntries(headers.map((header, index) => [header, cells[index]]));
});
}
''', args: [selector.selector, headerSelector, rowSelector, cellSelector]);
ctx.set(key, List<Map<String, String>>.from(tableData));
return this;
}