我需要分析此网页。。。。
http://monitorps.sardegnasalute.it/monitorps/MonitorServlet?page=carLavoroPresidi&tipoProntoSoccorso=TUTTI&codiceAziendaSanitaria=200102&idPresidio=102MAD02&indirizzo=null&idProntoSoccorso=30
... 使用PHP提取表格中“ROSSO”、“GIALLO”、“VERDE”和“BIANCO”列下的数字。
(注意:如果您尝试浏览该页面,您可能会在该页面中看到不同的值……这无关紧要……,它会自动更改……)
这些值是网页内的POST请求结果。
这是我用来使用curl发送POST请求的PHP代码,然后解析JSON响应(使用Skyscanner JSON路径..在我的代码中工作得很好..),尝试使用XPath解析提取值。
<?php
include "./tmp/vendor/autoload.php";
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => "http://monitorps.sardegnasalute.it/monitorps/MonitorServlet",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "idMacroArea=null&codiceAziendaSanitaria=200102&idAreaVasta=null&idPresidio=102MAD02&idProntoSoccorso=30&tipoProntoSoccorso=TUTTI&vicini=null&xhr=true",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"content-type: application/x-www-form-urlencoded"
),
));
$server_output = curl_exec ($ch);
curl_close ($ch);
$jsonObject = new JsonPath\JsonObject($server_output);
$jsonPathExpr = '$..view';
$res = $jsonObject->get($jsonPathExpr);
print $res[0];
$dom = new DOMDocument();
@$dom->loadHTML(json_encode($res[0]));
$xpath = new DOMXPath($dom);
$xpath_for_parsing = '/html/body/div[1]/div/div/div/table/tbody/tr[2]/td[4]';
$colorWaitingNumber = $xpath->query($xpath_for_parsing);
$theValue = 'N.D.';
foreach( $colorWaitingNumber as $node )
{
$theValue = $node->nodeValue;
}
print $theValue;
?>
结果如下图所示
其中表格是我的代码中命令的结果。。。
print $res[0];
和
N、 D
是我尝试解析以提取所需值之一时的结果
关于我正在使用的xpath,我已经用页面源代码验证了它。。。。。。
我哪里做错了?