2017-10-15 68 views
0

我正在學習處理DOMXpathphp。我正在使用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); 
+0

首先,我看不出有什麼 「$ resultado」 時,你的意思是這樣$ resultados [] = [ 'I'= > $ i,'t'=> $ t,'l'=> $ l]; //?其次,「預覽」是什麼意思? – konrados

+0

@konrados($ resultados [] = ['i'=> $ i,'t'=> $ t,'l'=> $ l]是否正確?)(預覽=捕獲項目列表) –

+0

不知道你的意思,你只是複製我的代碼,並在怪異的地方添加括號o_O :)無論如何,我發佈了一個答案,我不認爲'評論'會處理整個代碼。 – konrados

回答

0

好的,所以在這裏你的代碼有兩個更正。首先,我將元素添加到$ resultados的子數組中,並且使用foreach而不是print_r/var_dump來添加子元素

順便說一句,不是imdb提供API嗎?

<?php 
    ini_set('display_errors', 1); 
    error_reporting(-1); 

    $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')); 
    //$doc->loadHTML($HTML); 
    $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[] = ['i' => $i[0], 't' => $t[0], 'l' => $l[0]]; 

} 
// What command should I use to have a preview of the results and check if everything is ok? 
//var_dump($resultados); 
foreach($resultados as $r){ 
    echo "\n-----------\n"; 
    echo $r['i']->value."\n"; 
    echo $r['t']->textContent."\n"; 
    echo $r['l']->value."\n"; 
} 

你可以用它在這裏玩: https://3v4l.org/hal0G

+0

Imdb提供api。我只是用它來學習DOMXpath。 我仍然無法在本地主機上看到結果,即使進行了更改。 –

+0

@AntonioOliveira - 你是否打開了上面的示例鏈接,即https://3v4l.org/hal0G?其次,如果你看不到結果,那麼你看到了什麼?第三,啓用錯誤報告,我更新了我的代碼,即在頂部添加了兩行。 – konrados

+0

問題是由file_get_contents(或cURL)捕獲頁面。使用$ HTML(脫機)工作。謝謝!! –