如何从XPath php中的子级(HTML)获取属性值

时间:2018-08-21 23:33:47

标签: php html xpath

首先,我一直在寻找我需要的所有元素,现在我正尝试从子级获取属性值-标题,URL和图像-但始终出现错误-请帮助,我在做什么错?

function getContent($value)
    {
        $homepage = file_get_contents('https://www.youtube.com/results?search_query=' . $value);

        $doc = new DOMDocument();
        libxml_use_internal_errors(TRUE); //disable libxml errors

        //check if any html is actually returned
        if (!empty($homepage)) {

            //load
            $doc->loadHTML($homepage);

            //remove errors for yucky HTML
            libxml_clear_errors();

            //get DOMxPath
            $scriptXpath = new DOMXPath($doc);

            //get all the <li> elements
            $scriptRows = $scriptXpath->query('//*[@class="item-section"]/li[position()>1]');

            $videos = array();
            foreach ($scriptRows as $scriptRow) {

                $VideoTitle = $scriptRow->{'/div/div/div/h3/a/@title'};
                $VideoUrl = 'https://youtube.com' .$scriptRow->{'/div/div/div[2]/h3/a/@href'};
                $VideoImg =  $scriptRow->{'/div/div/div[1]/a/div/span/img/@src'};
              // add to the end of a array of videos
                $videos[] = [
                    'title' => $VideoTitle,
                    'url' => $VideoUrl,
                    'image' => $VideoImg,
                ];
            }
        }

ERRORS i am getting :

Notice: Undefined property: DOMElement::$/div/div/div/h3/a/@title Notice: Undefined property: DOMElement::$/div/div/div[2]/h3/a/@href Notice: Undefined property:DOMElement::$/div/div/div[1]/a/div/span/img/@src

1 个答案:

答案 0 :(得分:0)

使用'getAttribute()

解决

 $scriptRows = $scriptXpath->query('//*[@class="item-section"]/li[position()>1]/div/div/div/h3/a');

foreach ($scriptRows as $scriptRow) {

                $VideoTitle = $scriptRow->getAttribute("title");
                $VideoUrl = 'https://youtube.com' .$scriptRow->getAttribute("href");
                $videos[] = [
                    'title' => $VideoTitle,
                    'url' => $VideoUrl,
                    //'image' => $VideoImg,
                ];


            }
                        
 $scriptRows = $scriptXpath->query('//*[@class="item-section"]/li[position()>1]/div/div/div[1]/a/div/span/img');

          

  foreach ($scriptRows as $scriptRow) {
  // add to the end of a array of videos
    $VideoImg =  $scriptRow->getAttribute("src");
    $videos[] = [
      'image' => $VideoImg,
     ];
}