简体   繁体   English

DomXpath和foreach。 如何预览捕获的元素?

[英]DomXpath and foreach. How to get a preview of the captured elements?

I am learning to deal with DOMXpath in php . 我正在学习处理php DOMXpath I was using regex (but I was discouraged here in the stack when for html capture). 我正在使用regex (但是对于HTML捕获,我不建议在堆栈中使用)。 I confess that for me it is not so simple and the DOM has its limits (when there are spaces in tag names and also in error handling). 我承认,对我来说,这不是那么简单,并且DOM有其局限性(当标记名称中以及在错误处理中都有空格时)。 If someone can help me with the command in php to get a preview of the captured elements and check if everything is right, I would appreciate it. 如果有人可以通过php中的命令帮助我获取捕获的元素的预览并检查是否一切正确,我将不胜感激。 If you have suggestions for improving the code, you're welcome to do so.The code below was based on a question in Stackoverflow itself. 如果您有改进代码的建议,欢迎这样做。以下代码基于Stackoverflow本身的问题。

<?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);

OK, so here your code with two corrections. 好的,这里有两个更正的代码。 First I'm adding a subarray to $resultados with the elements, and seconds I'm making a foreach instead of print_r/var_dump 首先,我将一个带有元素的子数组添加到$ resultados中,然后几秒钟我在进行foreach而不是print_r / var_dump

BTW, doesn't imdb offer an API? 顺便说一句,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";
}

You can play with it here: https://3v4l.org/hal0G 您可以在这里使用它: https : //3v4l.org/hal0G

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM