我正在学习处理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