我正在學習處理DOMXpath
php
。我正在使用regex
(但是當我捕獲html時,我在這裏被阻止)。我承認對我來說並不那麼簡單,並且DOM
也有它的限制(當標籤名稱中有空格並且還有錯誤處理時)。如果有人可以用php中的命令來幫助我獲取捕獲的元素的預覽並檢查一切是否正確,我將不勝感激。如果您有改進代碼的建議,歡迎您這樣做。以下代碼基於Stackoverflow本身的問題。DomXpath和foreach。如何獲取捕獲的元素的預覽?
<?php
$doc = new DOMDocument;
libxml_use_internal_errors(true);
// Deleting whitespace (if any)
$doc->preserveWhiteSpace = false;
@$doc->loadHTML(file_get_contents ('http://www.imdb.com/search/title?certificates=us:pg_13&genres=comedy&groups=top_250'));
$xpath = new DOMXPath($doc);
// Starting from the root element
$grupos = $xpath->query(".//*[@class='lister-item mode-advanced']");
// Creating an array and then looping with the elements to be captured (image, title, and link)
$resultados = array();
foreach($grupos as $grupo) {
$i = $xpath->query(".//*[@class='loadlate']//@src", $grupo);
$t = $xpath->query(".//*[@class='lister-item-header']//a/text()", $grupo);
$l = $xpath->query(".//*[@class='lister-item-header']//a/@href", $grupo);
$resultados[] = $resultado;
}
// What command should I use to have a preview of the results and check if everything is ok?
print_r($resultados);
首先,我看不出有什麼 「$ resultado」 時,你的意思是這樣$ resultados [] = [ 'I'= > $ i,'t'=> $ t,'l'=> $ l]; //?其次,「預覽」是什麼意思? – konrados
@konrados($ resultados [] = ['i'=> $ i,'t'=> $ t,'l'=> $ l]是否正確?)(預覽=捕獲項目列表) –
不知道你的意思,你只是複製我的代碼,並在怪異的地方添加括號o_O :)無論如何,我發佈了一個答案,我不認爲'評論'會處理整個代碼。 – konrados