How do you extract from a website table?

조회 수: 33 (최근 30일)
Christopher Taylor
Christopher Taylor 2022년 6월 3일
답변: Toshiaki Takeuchi 2023년 10월 24일
I'm trying to extract data from the table on this page(http://www.newyorkschools.com/districts/nyc-district-11.html).
I've tried tp uses webread but it isn't quite working for me. I'm attempting to extract the school names and the grade level and them place that into an excel file. (Helping a friend starting a stem program)
How do you think I should do?
url ='http://www.newyorkschools.com/districts/nyc-district-7.html';
data = webread(url)
tree=htmlTree(url)
selector = 'School Name'
subtrees = findElement(tree,selector)
subtrees(:)

채택된 답변

Christopher Creutzig
Christopher Creutzig 2022년 6월 7일
The problem with this page is that it is not using an HTML <table> for the data you are looking for. Otherwise, you would be able to simply use readtable(url) or maybe readtable(url,TableIndex=2).
Also, the selector needs to follow what is found in the HTML source, which again in this particular case is not made easy. MATLAB does not control what you need in there.
Here's something to get you started with:
url ='http://www.newyorkschools.com/districts/nyc-district-7.html';
data = webread(url);
tree = htmlTree(data);
tabs = findElement(tree,"#myTabContent > div");
schools = tabs(1);
rows = findElement(schools,".p_div");
schoolnames = findElement(schools,".pp-col-40");
extractHTMLText(schoolnames)
ans = 38×1 string array
"School Name" "Academy For Public Relations" "Alfred E. Smith Vocational High School" "Bronx Academy Of Letters" "Community High School For Social Justice" "Foreign Language Academy Of Global Studies" "Health Opportunities Program" "Hostos-Lincoln Academy Of Science" "I.S. 184 Rafael C. Y. Molina School" "Is 222" "J.H.S. 151 Henry Lou Gehrig Junior High School" "Jhs 162 L. Rodriguez De Tio School" "Mott Haven Village Prep High School" "Ms 203" "Ms 223 The Labratory School Of Finance" "New Explorers High School" "P.S. 1 Courtland School" "P.S. 154 Jonathan D. Hyatt School" "P.S. 156 Benjamin Banneker School" "P.S. 157 Grove Hill School" "P.S. 161 Ponce De Leon School" "P.S. 18 John Peter Zenger School" "P.S. 220 Mott Haven Village School" "P.S. 25 Bilingual School" "P.S. 277" "P.S. 30 Wilton School" "P.S. 43 Jonas Bronck School" "P.S. 49 Willis Avenue School" "P.S. 5 Port Morris School" "P.S. 65 Mother Hale Academy"

추가 답변 (2개)

Seth Furman
Seth Furman 2022년 6월 6일

Toshiaki Takeuchi
Toshiaki Takeuchi 2023년 10월 24일
url = "https://www.mathworks.com/help/matlab/text-files.html";
T = readtable(url,TableSelector="//TABLE[contains(.,'readtable')]", ...
ReadVariableNames=false)
T = 4×2 table
Var1 Var2 ________________ ___________________________________________ "readtable" "Create table from file" "writetable" "Write table to file" "readtimetable" "Create timetable from file (Since R2019a)" "writetimetable" "Write timetable to file (Since R2019a)"

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by