使用domXpath获取父元素的当前ID

问题描述 投票:0回答:1

我正致力于在百度上获得域名排名。

我想要做的是在域出现时得到结果的位置,我设法得到域名,我的问题是位置。

当域出现在结果上时,我需要得到idresult 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);
        }
php curl domxpath
1个回答
1
投票

从文档中

$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

© www.soinside.com 2019 - 2024. All rights reserved.