我正在使用此类使用class =“job”抓取此表中所有td的内容。
$table01 = $salary->find('table.table01');
$rows = $table01[0]->find('td.job');
然后我用它输出它有效,但显然只输出它作为明文,我需要用它做更多...
foreach($table01[0]->find('td.job') as $element) {
$jobs .= $element->plaintext . '<br />';
}
最终我希望它输出到这种格式。请注意,a href正在使用作业名称并将 - 和/替换为 - 。
<tr>
<td class="small"> <a href="/graphic-artist-designer">Graphic Artist / Designer</a>
$23,755 – $55,335 </td>
</tr>
<tr>
<td class="small"> <a href="/sales-associate">Sales Associate</a><br />
$15,577 – $56,290 </td>
</tr>
<tr>
<td class="small"> <a href="/film-video-editor">Film / Video Editor</a><br />
$24,184 – $94,493 </td>
</tr>
这是我刮刮的表格
<table cellpadding="0" cellspacing="0" border="0" class="table01">
<tr>
<td class="head">Test</td>
<td class="job">
<a href="/Graphic_Artist_%2f_Designer" id="UniqueID1">Graphic Artist / Designer</a><br/>
$23,755 – $55,335
</td>
</tr>
<tr>
<td class="head">Test</td>
<td class="job">
<a href="/Sales_Associate" id="UniqueID2">Sales Associate</a><br/>
$15,577 – $56,290
</td>
</tr>
<tr>
<td class="head">Test</td>
<td class="job">
<a href="/Film_%2f_Video_Editor" id="UniqueID3">Film / Video Editor</a><br/>
$24,184 – $94,493
</td>
</tr>
</table>
答案 0 :(得分:0)
可能更适合使用正则表达式
<?php
$html=file_get_contents('1.html');
$jobs='';
if(preg_match_all("/<tr>.*?<td.*?>.*?<\/td>.*?<td\sclass=\"job\">.*?<a.+?href=\"(.+?)\".+?>(.*?)<\/a>(.*?)<\/td>.*?<\/tr>/ims", $html, $res))
{
foreach($res[1] as $i=>$uri)
{
$uri=strtolower(urldecode($uri));
$uri=preg_replace("/_\/_/",'-',$uri);
$uri=preg_replace("/_/",'-',$uri);
$jobs.='<tr><td class="small"> <a href="'.$uri.'">'.$res[2][$i].'</a>'.$res[3][$i].'</td></tr>'."\n";
}
}
echo $jobs;