簡體   English   中英

使用domXpath獲取父元素的當前ID

[英]Get the current ID of parent element using domXpath

我正在努力獲得百度域名的排名。

我想做的是在域出現時獲取結果的位置,我設法獲取域名,我的問題是位置。

當域出現在結果上時,我需要獲取result c-containerid (這是位置)。 希望你能幫助我。

謝謝。

$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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM