我正致力于在百度上获得域名排名。
我想要做的是在域出现时得到结果的位置,我设法得到域名,我的问题是位置。
当域出现在结果上时,我需要得到id
的result c-container
(这是位置)。希望你帮帮我。
谢谢。
$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);
}
从文档中
$item->parentNode->tagName
例
if($item->parentNode->tagName == "h2") {
$href = $item->getAttribute("href");
$text = trim(preg_replace("/[\r\n]+/", " ", $item->nodeValue));
$links[] = [
'href' => $href,
'text' => $text
];
}
来源:https://www.the-art-of-web.com/php/html-xpath-query/#section_3