[英]Get the current ID of parent element using domXpath
I'm working on getting the ranking of a domain on baidu. 我正在努力获得百度域名的排名。
What I'm trying to do is get the position of the result when the domain appear, I manage to get the domain name, my problem is the position. 我想做的是在域出现时获取结果的位置,我设法获取域名,我的问题是位置。
I need to get the id
(this is the position) of the result c-container
when the domain appears on the result. 当域出现在结果上时,我需要获取
result c-container
的id
(这是位置)。 hope you help me. 希望你能帮助我。
thanks. 谢谢。
$finder = new DomXPath($document);
$results = $finder->query("//*[contains(@class, 'result c-container')]");
if($element){
$data = array();
foreach ($results as $result) {
# code...
$as = $result->getElementsByTagName('a');
foreach ($as as $a){
if ($a->getAttribute('class') === 'c-showurl') {
$textUrl = $a->nodeValue;
if (($pos = strpos($textUrl, "}")) !== FALSE) {
$textUrl = substr($textUrl, $pos+1);
}
$domain = trimUrl($domain);
if(preg_match("/{$domain}/i", $textUrl)) {
$data['domain'] = $textUrl;
$data['id'] = ?
}
}
}
}
array_push($res, $data);
}else{
$data = array();
array_push($res, $data);
}
From the documentation 从文档中
$item->parentNode->tagName
exmaple 〔实施例
if($item->parentNode->tagName == "h2") {
$href = $item->getAttribute("href");
$text = trim(preg_replace("/[\r\n]+/", " ", $item->nodeValue));
$links[] = [
'href' => $href,
'text' => $text
];
}
source: https://www.the-art-of-web.com/php/html-xpath-query/#section_3 来源: https : //www.the-art-of-web.com/php/html-xpath-query/#section_3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.