Search code examples
phpcurldomxpath

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. 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);
        }

Solution

  • 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