[英]How to find data and get text from html table td element next to it, without attribute using PHP Simple DOM?
[英]PHP Simple HTML DOM - How to find the table by a particular value in TD?
我的桌子看起来像
<table width="100%" border="0" cellpadding="2" cellspacing="0">
<tr>
<td><strong>NPA/Area Code:</strong></td>
<td><a href="/area-code/area-code-229.asp">229</a></td>
<td><strong>NXX Use Type:</strong></td>
<td>LANDLINE</td>
</tr>
<tr>
<td><strong>NXX/Prefix:</strong></td>
<td>428</td>
<td><strong>NXX Intro Version:</strong></td>
<td>2000-10-31</td>
</tr>
</table>
没有id
或class
表太多了,因此很难找到我想要的表。 我正在考虑使用td
的文本来选择table
。 那可能吗? 因为我要从中抓取数据的站点被编码为这种方式。 我不确定如何使用简单HTML DOM操作代码,选择此表,然后选择td
的文本。 我知道如何在td
提取值,所以问题是如何选择我想要的特定表。 我要抓取数据的链接是, 抓取源
任何帮助表示赞赏。 谢谢。
我建议您为该表创建一个标记,因为您尝试在AreaCode/Prefix 229-428 Details
下获取该表,然后使用该表,然后指向下一个同级项,即您想要的特定表。 例:
$html = file_get_html('http://www.area-codes.com/exchange/exchange.asp?npa=229&nxx=428');
$table = null;
$needle = 'AreaCode/Prefix 229-428 Details';
foreach($html->find('h3') as $marker) {
if($marker->innertext == $needle) {
$table = $marker->next_sibling();
break;
}
}
$data = array();
if($table) {
foreach($table->children() as $k => $tr) {
foreach($tr->children as $td) {
$data[$k][] = $td->innertext;
}
}
}
echo '<pre>';
print_r($data);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.