このWebページを解析する必要があります。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>';
?>
任意の提案/例/代替私のコードですか?
? '(string)。$ greenWaitingNumber'は構文が悪いので、そのような' DOMElement'をエコーすることはできません。(Simple XMLを使用すると 'SimpleXMLElement' can) – Scuzzy
あなたは正しいです。白いページが表示され、Webコンソールに「エラー500」と表示されます。私はこの問題が... $ theText =(文字列)についてだと思う。$ greenWaitingNumber; .... $ npath->クエリが正しいかどうか分かりません(私がxwpathを "検査要素"のインタラクティブ関数を使用して取得したことに注意してください)。 – Cesare
xパスはaインデックス表記のために特定の値を得ることはできませんが、それらをすべて取得するには、最初にもっと一般的なものが必要です.. '/ html/body/div/div/div/div/ul/li [6]/span' – Scuzzy