[英]PHP Simple HTML DOM - How to find the table by a particular value in TD?
My table looks like, 我的桌子看起来像
<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>
There are so many tables with no id
or class
, so finding the one I want is so hard. 没有id
或class
表太多了,因此很难找到我想要的表。 I am thinking about to use the text in the td
to select the table
. 我正在考虑使用td
的文本来选择table
。 Is that possible? 那可能吗? Because the site I want to scrape data from is coded to be this way. 因为我要从中抓取数据的站点被编码为这种方式。 I am unsure how to manipulate the code with Simple HTML DOM, to select this table and then select the text within the td
. 我不确定如何使用简单HTML DOM操作代码,选择此表,然后选择td
的文本。 I know how to extract the value inside td
, so the question is how to select this particular table that I want. 我知道如何在td
提取值,所以问题是如何选择我想要的特定表。 The link I want to scrape data is, scrape source 我要抓取数据的链接是, 抓取源
Any help is appreciated. 任何帮助表示赞赏。 Thanks. 谢谢。
I suggest you create a marker for that table, since your trying to get the table below AreaCode/Prefix 229-428 Details
, then use that and then point to the next sibling which is that particular table that you want. 我建议您为该表创建一个标记,因为您尝试在AreaCode/Prefix 229-428 Details
下获取该表,然后使用该表,然后指向下一个同级项,即您想要的特定表。 Example: 例:
$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);
This question might be helpful. 这个问题可能会有所帮助。
Essentially, you can get the nth element as such: 本质上,您可以这样获得第n个元素:
$table = $html->find('table', 3);
So long as the number of tables doesn't change you can extend that to do what you want. 只要表的数量没有变化,您就可以扩展它来做您想做的事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.