Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 为什么查询没有';与DOM不匹配?_Php_Dom_Html Parsing - Fatal编程技术网

Php 为什么查询没有';与DOM不匹配?

Php 为什么查询没有';与DOM不匹配?,php,dom,html-parsing,Php,Dom,Html Parsing,这是我的密码: $res = file_get_contents("http://www.lenzor.com/photo/search/index/type/user/%D8%B9%D9%84%DB%8C//text/%D9%81%D8%A7%D8%B7%D9%85%D9%87"); $doc = new \DOMDocument(); @$doc->loadHTMLFile($res); $xpath = new \DOMXpath($doc); $links = $xpath-&g

这是我的密码:

$res = file_get_contents("http://www.lenzor.com/photo/search/index/type/user/%D8%B9%D9%84%DB%8C//text/%D9%81%D8%A7%D8%B7%D9%85%D9%87");

$doc = new \DOMDocument();
@$doc->loadHTMLFile($res);
$xpath = new \DOMXpath($doc);
$links = $xpath->query("//ul[@class='user_box']/li");
$result = array();
if (!is_null($links)) {
    foreach ($links as $link) {
        $href = $link->getAttribute('class');
        $result[] = [$href];
    }
}

print_r($result);
是我正在制作的内容。我的意思是这是echo$res的结果


好的,我的代码的结果是一个空数组。因此,
$links
为空,
foreach
将不会执行。为什么?为什么
//ul[@class='user\u box']/li
查询与DOM不匹配


预期结果是一个数组包含
li
s的class属性。

试试这个,希望对您有所帮助。代码中几乎没有错误

1。您应该像这样搜索
'/ul[@class=“user\u box clearfix”]/li'
,因为该HTML源的
class=“user\u box clearfix”
类属性包含两个类

2.您应该使用
loadHTML
而不是
loadHTMLFile

loadHTML($res);
$xpath=new\DOMXpath($doc);
$links=$xpath->query('//ul[@class=“user\u box clearfix”]/li');
$result=array();
如果(!is_null($links)){
foreach($links作为$link){
$href=$link->getAttribute('class');
$result[]=[$href];
}
}
打印(结果);

li的数组
的意思是
followers\u内容
?@SahilGulati Yes。。然而,这只是一个例子。我想查看代码的工作版本。
loadHTMLFile
loadsFILE@u_mulder我应该使用什么函数来加载HTML内容?你知道,有一本手册。你可以看。
<?php
ini_set('display_errors', 1);

libxml_use_internal_errors(true);
$res = file_get_contents("http://www.lenzor.com/photo/search/index/type/user/%D8%B9%D9%84%DB%8C//text/%D9%81%D8%A7%D8%B7%D9%85%D9%87");

$doc = new \DOMDocument();
$doc->loadHTML($res);
$xpath = new \DOMXpath($doc);
$links = $xpath->query('//ul[@class="user_box clearfix"]/li');
$result = array();
if (!is_null($links)) {
    foreach ($links as $link) {
        $href = $link->getAttribute('class');
        $result[] = [$href];
    }
}

print_r($result);