2017-10-15 34 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(脱机)工作。谢谢!! –