我正在尝试从一个网站上提取一些信息。我需要的信息包含在一个表中,我已经创建了一个查询来查找它。在Chrome中使用控制台时,我可以看到我需要的表是由表达式返回的。但是,当我设置PHP代码时,查询返回零。
这来自Chrome控制台

这是我的PHP代码
$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;有什么想法吗?这里我漏掉了什么?
发布于 2014-07-04 22:44:30
你真的不需要以div为目标。只需将表的id作为目标。考虑这个例子:
$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 -->
<h1><?php echo $title; ?></h1>
<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>https://stackoverflow.com/questions/24564268
复制相似问题