我需要解析此網頁https://www.galliera.it/118獲取彩條下的數字。在PHP中使用curl和xpath解析HTML頁面
這是我的代碼(不工作!)...
<?php
ini_set('display_errors', 1);
$url = 'https://www.galliera.it/118';
print "The url ... ".$url;
echo '<br>';
echo '<br>';
//#Set CURL parameters ...
$ch = curl_init();
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_PROXY, '');
$data = curl_exec($ch);
curl_close($ch);
//print "Data ... ".$data;
//echo '<br>';
//echo '<br>';
$dom = new DOMDocument();
@$dom->loadHTML($data);
$xpath = new DOMXPath($dom);
// This is the xpath for a number under a bar ....
// /html/body/div[2]/div[1]/div/div/ul/li[6]/span
// How may I get it?
// The following code doesn't work, it's only to show my goals ..
$greenWaitingNumber = $xpath->query('/html/body/div[2]/div[1]/div/div/ul/li[6]/span');
$theText = (string).$greenWaitingNumber;
print "Data ... ".$theText;
echo '<br>';
echo '<br>';
?>
任何建議/例子/替代品?
「不工作」 你能更具體?還有'(字符串)。$ greenWaitingNumber'是不好的語法,你不能像這樣回覆一個'DOMElement'(使用Simple XML時''SimpleXMLElement'可以) – Scuzzy
你是對的...對不起。白頁和Web控制檯顯示「錯誤500」。我認爲問題在於... $ theText =(string)。$ greenWaitingNumber; ....線性螺母我不太確定$ xpath->查詢是否正確(請注意,我在borwser中使用「Inspect元素」交互功能獲得了xpath ... – Cesare
您的x路徑對於因爲索引符號,具體的價值,但要得到他們所有你需要一些更通用的開始..'/ html/body/div/div/div/div/ul/li [6]/span' – Scuzzy