Import a table from a web site

조회 수: 5 (최근 30일)
Serena Sirigu
Serena Sirigu 2018년 1월 20일
답변: Rahul 2025년 6월 26일
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?

답변 (1개)

Rahul
Rahul 2025년 6월 26일
I understand that you wish to find the table element from the given web site. Consider the following approach:
  • '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.
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:
Thanks.

카테고리

Help CenterFile Exchange에서 Web Services에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by