Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.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 XPath中数据属性链接的文本值_Php_Dom_Xpath - Fatal编程技术网

Php 获取DOM XPath中数据属性链接的文本值

Php 获取DOM XPath中数据属性链接的文本值,php,dom,xpath,Php,Dom,Xpath,我尝试了许多xpath表达式、求值、循环等等。 我得到的最好结果是 " } object(DOMNodeList)#3 (1) { ["length"]=> int(0) } 有人告诉我做错了什么,让我摆脱了痛苦 $doc = new DOMDocument; libxml_use_internal_errors(true); $doc->preserveWhiteSpace = false; $doc->strictErrorChecking = false; $doc-&

我尝试了许多xpath表达式、求值、循环等等。 我得到的最好结果是

" } object(DOMNodeList)#3 (1) { ["length"]=> int(0) }
有人告诉我做错了什么,让我摆脱了痛苦

$doc = new DOMDocument;
libxml_use_internal_errors(true);
$doc->preserveWhiteSpace = false;
$doc->strictErrorChecking = false;
$doc->recover = true;
$text = urlencode('dog show');
$html = file_get_contents('https://en.wikipedia.org/w/index.php?search=' . $text . '&title=Special:Search&fulltext=Search');
$doc->loadHTML(htmlspecialchars($html));

var_dump($doc);
把一切都带回来,没有问题--

现在,如何将第一个搜索结果作为a href
/wiki/Dog_show
的文本值以及节点列表中嵌入的
标题
或span值取回

我已尝试将目标锁定在数据属性
data serp pos=“0”
,该属性包含我要查找的内容

$query = "//a/@href[data-serp-pos=\"0\"]";
$v = $xpath->evaluate($query);
var_dump($v);
我甚至试着在DOM树上再往上爬

// $query = '//*[@id="mw-content-text"]/div/ul/li[1]/div[1]/a';
// $query = '//*[@id="mw-content-text"]/div/ul/li[1]';
// $query = '//div[@id="mw-content-text"]//a/@href';
尝试循环

// $result = '';
// foreach ($xpath->evaluate($query) as $p) {
//   $result .= $dom->saveHtml($p);
// }
// var_dump($result);
在评估中添加
string
->nodeValue
->项(0)

长度始终为0

整个DIV html如下所示

<div class="mw-search-result-heading"><a href="/wiki/Dog_show" title="Dog show" data-serp-pos="0"><span class="searchmatch">Dog</span><span class="searchmatch">show</span></a></div>


对于获取
href
值和相关链接文本(或title属性——在本例中相同)而言,我没有做的(可能很简单)解决方案是什么?

我经常发现最容易“检查”我希望使用Chrome中的开发人员工具以元素为目标,从中可以复制以该特定节点为目标的XPath表达式。这并不总是返回最有用的XPath表达式,但它通常是一个很好的起点——在本例中,我调整了返回的查询并添加了类名

希望能有帮助

$term='dog show';
$url=sprintf('https://en.wikipedia.org/w/index.php?search=%s&title=Special:Search&fulltext=Search', urlencode( $term ) );


printf( '<a href="%s" target="_blank">%s</a>', $url, $url );

libxml_use_internal_errors(true);
$dom=new DOMDocument;
$dom->recover=true;
$dom->formatOutput=true;
$dom->preserveWhiteSpace=true;
$dom->strictErrorChecking=false;

$dom->loadHTMLFile( $url );
$xp=new DOMXPath( $dom );

/* possibly the important bit */
$query='//*[@id="mw-content-text"]/div/ul/li/div[@class="mw-search-result-heading"]/a';

$col=$xp->query( $query );

$html=array();

if( $col && $col->length > 0 ){
    foreach( $col as $node ){
        $html[]=array(
            'title'=>$node->nodeValue,
            'href'=>$node->getAttribute('href')
        );
    }
}


printf('<pre>%s</pre>',print_r($html,true));

我经常发现,使用Chrome中的开发人员工具“检查”我希望针对的元素是最容易的,因为可以从中复制针对该特定节点的XPath表达式。这并不总是返回最有用的XPath表达式,但它通常是一个很好的起点——在本例中,我调整了返回的查询并添加了类名

希望能有帮助

$term='dog show';
$url=sprintf('https://en.wikipedia.org/w/index.php?search=%s&title=Special:Search&fulltext=Search', urlencode( $term ) );


printf( '<a href="%s" target="_blank">%s</a>', $url, $url );

libxml_use_internal_errors(true);
$dom=new DOMDocument;
$dom->recover=true;
$dom->formatOutput=true;
$dom->preserveWhiteSpace=true;
$dom->strictErrorChecking=false;

$dom->loadHTMLFile( $url );
$xp=new DOMXPath( $dom );

/* possibly the important bit */
$query='//*[@id="mw-content-text"]/div/ul/li/div[@class="mw-search-result-heading"]/a';

$col=$xp->query( $query );

$html=array();

if( $col && $col->length > 0 ){
    foreach( $col as $node ){
        $html[]=array(
            'title'=>$node->nodeValue,
            'href'=>$node->getAttribute('href')
        );
    }
}


printf('<pre>%s</pre>',print_r($html,true));

专业回答!非常干净的代码和非常理想的输出。我尝试使用内置的chrome xpath finder,但显然不正确,我必须调查原因。。通过将所有的reuslts放入一个数组(为什么不,对吗?)以及对
->nodeValue
->getAttribute
的一些深入了解,我希望返回的内容以及通过
$html[0]['title']
/
$html[0]['href']
轻松定义的索引结果,第一个结果
$html[1]['title']
/
$html[1]['href']
第二个,等等。非常好,谢谢!专业回答!非常干净的代码和非常理想的输出。我尝试使用内置的chrome xpath finder,但显然不正确,我必须调查原因。。通过将所有的reuslts放入一个数组(为什么不,对吗?)以及对
->nodeValue
->getAttribute
的一些深入了解,我希望返回的内容以及通过
$html[0]['title']
/
$html[0]['href']
轻松定义的索引结果,第一个结果
$html[1]['title']
/
$html[1]['href']
第二个,等等。非常好,谢谢!