- 'webread' alone won't be able to process the html content on the page and you would require function 'htmltree' to parse the html.
- Now using the 'findElement' obtain the table component.
- Is your require to obtain each data from the table, consider using 'findElement' on 'tr', 'td' tags of the table.
Import a table from a web site
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hi everyone! I am trying to import a table from a web site http://www.sardegnacedoc.it/idrografico/elaborazioni/426400/33147/20207/ I am trying to use webread but I can't import just the table that I need. Is there a way to import the table, maybe using the Xpath?
0 comentarios
Respuestas (1)
Rahul
el 26 de Jun. de 2025
I understand that you wish to find the table element from the given web site. Consider the following approach:
Here is an example:
url = 'http://www.sardegnacedoc.it/idrografico/elaborazioni/426400/33147/20207/';
htmlContent = webread(url);
tree = htmlTree(htmlContent);
% Find all tables
tables = findElement(tree, "table");
targetTable = tables(1); % % Pick the one you need (Change index if needed)
rows = findElement(targetTable, "tr");
data = [];
for i = 1:length(rows)
tdCells = findElement(rows(i), "td");
thCells = findElement(rows(i), "th");
cells = [tdCells; thCells];
% Sort to preserve order if needed (optional)
% Extract text
rowData = strings(1, numel(cells));
for j = 1:numel(cells)
rowData(j) = extractHTMLText(cells(j));
end
data = [data; rowData]; % Append the row
end
disp(data)
This approach works in your case.
I tried doing the same using 'xpath' parsers and was getting errors like: "The element type "meta" must be terminated by the matching end-tag "</meta>"." This is possibly as the html being obtained is not a well-formed XML.
The following MathWorks documentations can be referred:
'matlab.io.xml.dom.Parser Class': https://www.mathworks.com/help/matlab/ref/matlab.io.xml.dom.parser-class.html
Thanks.
0 comentarios
Ver también
Categorías
Más información sobre Web Services en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!