3
我使用PHP的DomDocument並試圖刮出來的東西,看起來像這樣:獲取有itemprop的屬性的所有元素
<div itemprop='movie'>Fight Club</div>
它也可能是這樣的:
<span itemprop='musician'>Ozzy Osbourne</span>
我想抓取頁面上的所有itemprop='n'
,並將它們放入數組中以存儲它們的nodevalue以及相關的itemprop名稱。到目前爲止我的代碼看起來是這樣的:
function getItemprops(){
foreach($this->dom->getAttribute("itemprop") as $buffer) {
$itempropList = array(
'theNodeValue' => $buffer->nodeValue,
'theItemprop' => $buffer->getAttribute("itemprop")
)
return $itempropList;
}
}
我的代碼應該沿線的某處得到一個數組:
array (
array(
0 =>
"theNodeValue" => "Fight Club",
"theItemprop" => "movie"
1 =>
"theNodeValue" => "Fight Club",
"theItemprop" => "movie"
)
)
不幸的是,我的代碼返回Fatal error: Call to undefined method DOMDocument::getAttribute()
。
所以基本上,我想選擇所有itemprop=""
的並將它們放入數組中。
感謝您的幫助!
非常感謝! –