如何通过class获取cell?
How to get cell by class?
我正在尝试迭代具有以下组成的 table:
<table><thead><tr class="sub-head"></tr></thead><tbody><tr class="group-head expanded live" id="date_matches-109-45564" stage-value="1"><th colspan="5"><h3><span class="flag_16 left_16 japan_16_left">Japan - J1 League</span></h3></th><th class="competition-link"> <a href="/national/japan/j1-league/2018/regular-season/r45564/"><span>More…</span></a></th></tr><tr class="even highlight expanded first last match no-date-repetition" data-timestamp="1526803200" id="block_home_matches_30_match-2722312" data-competition="109"><td class="minute visible">89'</td><td class="team team-a "><a href="/teams/japan/vissel-kobe/1328/" title="Vissel Kobe">Vissel Kobe</a></td><td class="score-time score"><a href="/matches/2018/05/20/japan/j1-league/vissel-kobe/consadole-sapporo/2722312/?ICID=HP_MS_01_01">4 - 0</a></td><td class="team team-b "><a href="/teams/japan/consadole-sapporo/1347/" title="Consadole Sapporo">Consadole Sapporo</a></td><td class="events-button button first-occur"><a href="/matches/2018/05/20/japan/j1-league/vissel-kobe/consadole-sapporo/2722312/#events" title="View events" class="events-button-button ">View events</a></td><td class="info-button button"><a href="/matches/2018/05/20/japan/j1-league/vissel-kobe/consadole-sapporo/2722312/" title="More info">More info</a></td></tr><tr class="group-head live expanded loaded" id="date_matches-110-45567" stage-value="2"><th colspan="5"><h3><span class="flag_16 left_16 japan_16_left">Japan - J2 League</span></h3></th><th class="competition-link"> <a href="/national/japan/j2-league/2018/regular-season/r45567/"><span>More…</span></a></th></tr> <tr class="even highlight expanded first last match no-date-repetition" data-timestamp="1526803200" id="block_home_matches_30_match-2721813" data-competition="110"><td class="minute visible">90'</td><td class="team team-a "><a href="/teams/japan/ehime-fc/6521/" title="Ehime">Ehime</a></td><td class="score-time score"><a href="/matches/2018/05/20/japan/j2-league/ehime-fc/kyoto-sanga/2721813/?ICID=HP_MS_01_01">1 - 2</a></td><td class="team team-b "><a href="/teams/japan/kyoto-sanga/1340/" title="Kyoto Sanga">Kyoto Sanga</a></td><td class="events-button button first-occur"><a href="/matches/2018/05/20/japan/j2-league/ehime-fc/kyoto-sanga/2721813/#events" title="View events" class="events-button-button ">View events</a></td><td class="info-button button"><a href="/matches/2018/05/20/japan/j2-league/ehime-fc/kyoto-sanga/2721813/" title="More info">More info</a></td></tr>
<tr class="group-head expanded live" id="date_matches-1110-45216" stage-value="3"><th colspan="5"><h3><span class="flag_16 left_16 korea-republic_16_left">Korea Republic - K League 2</span></h3></th><th class="competition-link"> <a href="/national/korea-republic/k-league/2018/regular-season/r45216/"><span>More…</span></a></th></tr><tr class="even highlight expanded first last match no-date-repetition" data-timestamp="1526806800" id="block_home_matches_30_match-2725079" data-competition="1110"><td class="minute visible">HT</td><td class="team team-a "><a href="/teams/korea-republic/anyang/2989/" title="Anyang">Anyang</a></td><td class="score-time score"><a href="/matches/2018/05/20/korea-republic/k-league/anyang/gwangju-fc/2725079/?ICID=HP_MS_03_01">0 - 1</a></td><td class="team team-b "><a href="/teams/korea-republic/gwangju-fc/18351/" title="Gwangju">Gwangju</a></td><td class="events-button button first-occur"><a href="/matches/2018/05/20/korea-republic/k-league/anyang/gwangju-fc/2725079/#events" title="View events" class="events-button-button ">View events</a></td><td class="info-button button"><a href="/matches/2018/05/20/korea-republic/k-league/anyang/gwangju-fc/2725079/" title="More info">More info</a></td></tr><tr class="ad"><td colspan="6"><div class="block_ad ad_date_matches block clearfix"></div></td></tr></tbody></table>
我以这种方式将 html
加载到 DOMDocument
中:
$dom = new DOMDocument();
$dom->loadHTML("string which contains the table");
所以我得到所有包含class match
的tr,一共有3
tr:
$xpath = new DOMXPath($dom);
$rows = $xpath->query("//tr[contains(@class, 'match')]");
开始 foreach:
foreach($rows as $tr)
{
$c = $tr->find('td.minute');
echo $c;
}
我试图访问包含 class minute
的 cell
,但出现此错误:
Call to undefined method DOMElement::find()
我做错了什么?
以下是如何在 class
分钟内找到所有单元格(来自给定的 $tr
):
$cells = $xpath->query("./td[contains(@class, 'minute')]", $tr);
$rows = $xpath->query("//tr[contains(@class, 'match')]");
foreach($rows as $row) {
$td = $xpath->query("./td[contains(@class, 'minute')]", $row);
echo $td->item(0)->textContent . "<br>";
}
我正在尝试迭代具有以下组成的 table:
<table><thead><tr class="sub-head"></tr></thead><tbody><tr class="group-head expanded live" id="date_matches-109-45564" stage-value="1"><th colspan="5"><h3><span class="flag_16 left_16 japan_16_left">Japan - J1 League</span></h3></th><th class="competition-link"> <a href="/national/japan/j1-league/2018/regular-season/r45564/"><span>More…</span></a></th></tr><tr class="even highlight expanded first last match no-date-repetition" data-timestamp="1526803200" id="block_home_matches_30_match-2722312" data-competition="109"><td class="minute visible">89'</td><td class="team team-a "><a href="/teams/japan/vissel-kobe/1328/" title="Vissel Kobe">Vissel Kobe</a></td><td class="score-time score"><a href="/matches/2018/05/20/japan/j1-league/vissel-kobe/consadole-sapporo/2722312/?ICID=HP_MS_01_01">4 - 0</a></td><td class="team team-b "><a href="/teams/japan/consadole-sapporo/1347/" title="Consadole Sapporo">Consadole Sapporo</a></td><td class="events-button button first-occur"><a href="/matches/2018/05/20/japan/j1-league/vissel-kobe/consadole-sapporo/2722312/#events" title="View events" class="events-button-button ">View events</a></td><td class="info-button button"><a href="/matches/2018/05/20/japan/j1-league/vissel-kobe/consadole-sapporo/2722312/" title="More info">More info</a></td></tr><tr class="group-head live expanded loaded" id="date_matches-110-45567" stage-value="2"><th colspan="5"><h3><span class="flag_16 left_16 japan_16_left">Japan - J2 League</span></h3></th><th class="competition-link"> <a href="/national/japan/j2-league/2018/regular-season/r45567/"><span>More…</span></a></th></tr> <tr class="even highlight expanded first last match no-date-repetition" data-timestamp="1526803200" id="block_home_matches_30_match-2721813" data-competition="110"><td class="minute visible">90'</td><td class="team team-a "><a href="/teams/japan/ehime-fc/6521/" title="Ehime">Ehime</a></td><td class="score-time score"><a href="/matches/2018/05/20/japan/j2-league/ehime-fc/kyoto-sanga/2721813/?ICID=HP_MS_01_01">1 - 2</a></td><td class="team team-b "><a href="/teams/japan/kyoto-sanga/1340/" title="Kyoto Sanga">Kyoto Sanga</a></td><td class="events-button button first-occur"><a href="/matches/2018/05/20/japan/j2-league/ehime-fc/kyoto-sanga/2721813/#events" title="View events" class="events-button-button ">View events</a></td><td class="info-button button"><a href="/matches/2018/05/20/japan/j2-league/ehime-fc/kyoto-sanga/2721813/" title="More info">More info</a></td></tr>
<tr class="group-head expanded live" id="date_matches-1110-45216" stage-value="3"><th colspan="5"><h3><span class="flag_16 left_16 korea-republic_16_left">Korea Republic - K League 2</span></h3></th><th class="competition-link"> <a href="/national/korea-republic/k-league/2018/regular-season/r45216/"><span>More…</span></a></th></tr><tr class="even highlight expanded first last match no-date-repetition" data-timestamp="1526806800" id="block_home_matches_30_match-2725079" data-competition="1110"><td class="minute visible">HT</td><td class="team team-a "><a href="/teams/korea-republic/anyang/2989/" title="Anyang">Anyang</a></td><td class="score-time score"><a href="/matches/2018/05/20/korea-republic/k-league/anyang/gwangju-fc/2725079/?ICID=HP_MS_03_01">0 - 1</a></td><td class="team team-b "><a href="/teams/korea-republic/gwangju-fc/18351/" title="Gwangju">Gwangju</a></td><td class="events-button button first-occur"><a href="/matches/2018/05/20/korea-republic/k-league/anyang/gwangju-fc/2725079/#events" title="View events" class="events-button-button ">View events</a></td><td class="info-button button"><a href="/matches/2018/05/20/korea-republic/k-league/anyang/gwangju-fc/2725079/" title="More info">More info</a></td></tr><tr class="ad"><td colspan="6"><div class="block_ad ad_date_matches block clearfix"></div></td></tr></tbody></table>
我以这种方式将 html
加载到 DOMDocument
中:
$dom = new DOMDocument();
$dom->loadHTML("string which contains the table");
所以我得到所有包含class match
的tr,一共有3
tr:
$xpath = new DOMXPath($dom);
$rows = $xpath->query("//tr[contains(@class, 'match')]");
开始 foreach:
foreach($rows as $tr)
{
$c = $tr->find('td.minute');
echo $c;
}
我试图访问包含 class minute
的 cell
,但出现此错误:
Call to undefined method DOMElement::find()
我做错了什么?
以下是如何在 class
分钟内找到所有单元格(来自给定的 $tr
):
$cells = $xpath->query("./td[contains(@class, 'minute')]", $tr);
$rows = $xpath->query("//tr[contains(@class, 'match')]");
foreach($rows as $row) {
$td = $xpath->query("./td[contains(@class, 'minute')]", $row);
echo $td->item(0)->textContent . "<br>";
}