我正在努力获得一个域名在百度的排名。
我想要做的是在域名出现时获得结果的位置,我设法获得域名,我的问题是位置。
当域名出现在结果上时,我需要获取result c-container的id(这是位置)。希望你能帮我。
谢谢。
$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);
}发布于 2019-04-15 17:38:18
从文档中
$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
https://stackoverflow.com/questions/55686217
复制相似问题