我有一个包含许多标题的表格:
<TR>
<TH CLASS="ddtitle" scope="colgroup" ><A HREF="http://foo.com">Linked text</A></TH>
</TR>
该表长达数千行,因此我无法完全共享,但这里是初始标记和表中的一个完整项目。可悲的是,每个项目都没有嵌套,而且评论是我的 - 所以解读一个项目开始和结束的位置是一件痛苦的事。
<TABLE CLASS="datadisplaytable" SUMMARY="Layout table" width="100%"><CAPTION class="captiontext">Items Found</CAPTION>
<!-- START of first item in the table -->
<TR>
<TH CLASS="ddtitle" scope="colgroup" ><A HREF="http://foo.com">Linked text</A></TH>
</TR>
<TR>
<TD CLASS="dddefault">
<SPAN class="fieldlabeltext">Term: </SPAN>Fall
<BR>
<SPAN class="fieldlabeltext">Registration: </SPAN>Jan 1, 2018 to Aug 1, 2018
<BR>
<SPAN class="fieldlabeltext">Levels: </SPAN>Undergraduate
<BR>
<BR>
Location
<BR>
Lecture Schedule Type
<BR>
3.000 Credits
<BR>
<A HREF="foo">View Entry</A>
<BR>
<BR>
<TABLE CLASS="datadisplaytable" SUMMARY="Meeting time table"><CAPTION class="captiontext">Scheduled Meeting Times</CAPTION>
<TR>
<TH CLASS="ddheader" scope="col" >Type</TH>
<TH CLASS="ddheader" scope="col" >Time</TH>
<TH CLASS="ddheader" scope="col" >Days</TH>
<TH CLASS="ddheader" scope="col" >Where</TH>
<TH CLASS="ddheader" scope="col" >Date Range</TH>
<TH CLASS="ddheader" scope="col" >Schedule Type</TH>
<TH CLASS="ddheader" scope="col" >Instructors</TH>
</TR>
<TR>
<TD CLASS="dddefault">Lecture</TD>
<TD CLASS="dddefault">9:20 am - 10:10 am</TD>
<TD CLASS="dddefault">MWF</TD>
<TD CLASS="dddefault">Some Building Room 101</TD>
<TD CLASS="dddefault">Aug 1, 2018 - Dec 1, 2018</TD>
<TD CLASS="dddefault">Lecture</TD>
<TD CLASS="dddefault">Instructor Name (<ABBR title= "Primary">P</ABBR>)<A HREF="mailto:email@foo.com" target="Instructur Name" ><IMG SRC="/wtlgifs/email.png" ALIGN="middle" ALT="E-mail" CLASS="headerImg" TITLE="E-mail" NAME="web_email" HSPACE=0 VSPACE=0 BORDER=0 HEIGHT=16 WIDTH=16></A></TD>
</TR>
</TABLE>
<BR>
<BR>
</TD>
</TR>
<!-- END first item in the table -->
我想提取项目详细信息,从课程名称(文本内容,“链接文本”,th.ddtitle内部)和课程链接(这是th.ddtitle中的href)开始。这是我尝试抓住这两个项目的原因:
$dom = new DOMDocument();
$myHtml = file_get_contents(__DIR__.'myfile.html');
$dom->loadHTML($myHtml);
$xpath = new DOMXPath($dom);
// first part changes an outer table with the same class, so I can get inner tables without the outer one
$tables = $xpath->query("//table[@class='datadisplaytable']");
for($i=0; $i<1; $i++) {
$tables[$i]->setAttribute('class', 'masterTable');
}
$html = $dom->saveHTML();
// now, the query I'm having trouble with:
$textAndLink = $xpath->query("//th[@class='ddtitle']/*");
$i=1;
foreach($textAndLink as $info) {
foreach($info->childNodes as $child) {
if($i%2 == 0) {
echo $child->getAttribute('href') . '<br>';
} else {
echo $child->nodeValue . '<br>';
}
}
$i++;
}
我还尝试了print_r($child)
,显示的唯一项目是文本节点,没有<a>
标记。如何同时获得锚点的“href”属性和文本内容?我对上面代码的期望是这样的列表:
http://foo.com/<br>
Linked text<br>
http://foo.com/secondlink<br>
Second linked text<br>
依此类推。
答案 0 :(得分:0)
<?php
ini_set('display_errors', 1);
$string = '
<TABLE CLASS="datadisplaytable" SUMMARY="Layout table" width="100%"><CAPTION class="captiontext">Items Found</CAPTION>
<!-- START of first item in the table -->
<TR>
<TH CLASS="ddtitle" scope="colgroup" ><A HREF="http://foo.com">Linked text</A></TH>
</TR>
<TR>
<TD CLASS="dddefault">
<SPAN class="fieldlabeltext">Term: </SPAN>Fall
<BR>
<SPAN class="fieldlabeltext">Registration: </SPAN>Jan 1, 2018 to Aug 1, 2018
<BR>
<SPAN class="fieldlabeltext">Levels: </SPAN>Undergraduate
<BR>
<BR>
Location
<BR>
Lecture Schedule Type
<BR>
3.000 Credits
<BR>
<A HREF="foo">View Entry</A>
<BR>
<BR>
<TABLE CLASS="datadisplaytable" SUMMARY="Meeting time table"><CAPTION class="captiontext">Scheduled Meeting Times</CAPTION>
<TR>
<TH CLASS="ddheader" scope="col" >Type</TH>
<TH CLASS="ddheader" scope="col" >Time</TH>
<TH CLASS="ddheader" scope="col" >Days</TH>
<TH CLASS="ddheader" scope="col" >Where</TH>
<TH CLASS="ddheader" scope="col" >Date Range</TH>
<TH CLASS="ddheader" scope="col" >Schedule Type</TH>
<TH CLASS="ddheader" scope="col" >Instructors</TH>
</TR>
<TR>
<TD CLASS="dddefault">Lecture</TD>
<TD CLASS="dddefault">9:20 am - 10:10 am</TD>
<TD CLASS="dddefault">MWF</TD>
<TD CLASS="dddefault">Some Building Room 101</TD>
<TD CLASS="dddefault">Aug 1, 2018 - Dec 1, 2018</TD>
<TD CLASS="dddefault">Lecture</TD>
<TD CLASS="dddefault">Instructor Name (<ABBR title= "Primary">P</ABBR>)<A HREF="mailto:email@foo.com" target="Instructur Name" ><IMG SRC="/wtlgifs/email.png" ALIGN="middle" ALT="E-mail" CLASS="headerImg" TITLE="E-mail" NAME="web_email" HSPACE=0 VSPACE=0 BORDER=0 HEIGHT=16 WIDTH=16></A></TD>
</TR>
</TABLE>
<BR>
<BR>
</TD>
</TR>';
$domDocument = new DOMDocument();
$domDocument->loadHTML($string);
$domXPath = new DOMXPath($domDocument);
$results = $domXPath->query('//tr/th[@class="ddtitle"]/a');
foreach($results as $result)
{
print_r($result->textContent);
print_r($result->getAttribute("href"));
}