DOMXPath Return Zero
我正在尝试从网站中提取一些信息。我需要的信息包含在一个表中,我已经创建了一个查询来查找它。从Chrome使用控制台时,我可以看到表达式返回了我需要的表。但是当我设置PHP代码时,查询返回零。
这是从Chrome控制台
这是我的PHP代码
1 2 3 4 5 6 7 8 9 | $ch = curl_init($domain); curl_setopt($ch,CURLOPT_RETURNTRANSFER, true); $cl = curl_exec($ch); $dom = new DOMDocument(); @$dom->loadHTML($cl); $xpath = new DOMXPath($dom); $table = $xpath->query("//div[@id='content_fmainplace']//form/table/tbody/tr[15]//table"); echo $table->length; |
有什么想法吗?我在这里想念什么?
您真的不需要定位div。只需将目标定位为表的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | $domain = 'http://app.cfe.gob.mx/Aplicaciones/CCFE/Tarifas/Tarifas/tarifas_casa.asp?Tarifa=DACTAR1E&Temporada4=Verano&Anio=2014&imprime=&Periodo=4&mes2=a+septiembre.&mes=1'; $ch = curl_init($domain); curl_setopt($ch,CURLOPT_RETURNTRANSFER, true); $cl = curl_exec($ch); $dom = new DOMDocument(); libxml_use_internal_errors(true); $dom->loadHTML($cl); libxml_clear_errors(); $xpath = new DOMXPath($dom); // target the title $title = $values = $xpath->query('//table[@id="Table1"]/tr[1]/td[1]/form/table/tr[14]')->item(0)->nodeValue; // title rows $rows = $xpath->query('//table[@id="Table1"]/tr[1]/td[1]/form/table/tr[15]/td/table/tr'); $row_values = array(); // process td elements foreach($rows as $index => $row) { foreach($row->childNodes as $td) { // clean up $row_values[$index][] = preg_replace( '/\\s+/', '', trim($td->nodeValue)); } // clean up again $row_values[$index] = array_filter($row_values[$index]); } ?> <!-- print them --> <?php echo $title; ?> <table cellpadding="10"> <?php foreach($row_values as $values): ?> <tr><?php foreach($values as $value): ?> <td><?php echo $value; ?></td> <?php endforeach; ?></tr> <?php endforeach; ?> </table> |