当前位置: 首页>>代码示例>>PHP>>正文


PHP DOMDocument::querySelector方法代码示例

本文整理汇总了PHP中DOMDocument::querySelector方法的典型用法代码示例。如果您正苦于以下问题:PHP DOMDocument::querySelector方法的具体用法?PHP DOMDocument::querySelector怎么用?PHP DOMDocument::querySelector使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在DOMDocument的用法示例。


在下文中一共展示了DOMDocument::querySelector方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: finalizePod

 /**
  * @inheritdoc
  */
 protected function finalizePod(Pod $pod, DOMDocument $document, $response)
 {
     $container = $document->querySelector('.container .leftCol .section.main_profile');
     if (!$container) {
         throw new ProviderException('Container wrapper is missing in document.');
     }
     foreach ($container->childNodes as $child) {
         if ($child->nodeType != XML_ELEMENT_NODE) {
             continue;
         }
         switch ($child->tagName) {
             case 'h1':
                 // title
                 $pod->title = $child->textContent;
                 break;
             case 'h5':
                 // date
                 $pod->date = $this->parseDate($child->textContent, '%d %B %Y');
                 break;
             case 'span':
                 // image url
                 if ($child->hasChildNodes()) {
                     $img = $child->lastChild;
                     if ($img->tagName == 'img') {
                         $src = $img->getAttribute('src');
                         if (($pos = strrpos($src, '?')) !== false) {
                             $src = substr($src, 0, $pos);
                         }
                         $pod->imageUrl = $src;
                     }
                 }
                 break;
             case 'div':
                 // description
                 if (!$pod->desc && $child->hasChildNodes()) {
                     foreach ($child->childNodes as $node) {
                         if ($node->nodeType == XML_ELEMENT_NODE && $node->tagName == 'p') {
                             $pod->desc .= $node->textContent . "\n";
                         }
                     }
                     $pod->desc = trim($pod->desc);
                 }
                 break;
         }
     }
 }
开发者ID:skoro,项目名称:pod,代码行数:49,代码来源:Biomedical.php

示例2: finalizePod

 /**
  * @inheritdoc
  */
 protected function finalizePod(Pod $pod, DOMDocument $document, $response)
 {
     $img = $document->querySelector('.oneHalf.gutter .piccentre img');
     if (!$img) {
         return;
     }
     $src = $img->getAttribute('src');
     // Replace image to high resolution.
     $src = preg_replace('/c\\.jpg$/', 'k.jpg', $src);
     $pod->imageUrl = $src;
     // Description from the image's "alt" attribute.
     $pod->desc = $img->getAttribute('alt');
     // Get direct link to page.
     if ($link = $img->parentNode->getAttribute('href')) {
         $components = parse_url($this->url);
         $pod->baseUrl = $components['scheme'] . '://' . $components['host'] . $link;
     }
 }
开发者ID:skoro,项目名称:pod,代码行数:21,代码来源:Telegraph.php

示例3: parsePodBaseUrl

 /**
  * @inheritdoc
  */
 protected function parsePodBaseUrl(DOMDocument $document)
 {
     $elem = $document->querySelector('#alpha-inner h3.entry-header a');
     return $elem ? $elem->getAttribute('href') : '';
 }
开发者ID:skoro,项目名称:pod,代码行数:8,代码来源:EarthScience.php

示例4: getDateFromDocument

 /**
  * Parse document date to POD date.
  * @param DOMDocument $document
  * @param string $selector CSS selector with date.
  * @param string $format date format {@link http://docs.php.net/manual/en/function.strftime.php}
  * @throws ProviderException when no date element found or format doesn't to date string.
  * @return string date in format YYYY-MM-DD
  */
 protected function getDateFromDocument(\DOMDocument $document, $selector, $format)
 {
     if (!($elem = $document->querySelector($selector))) {
         throw new ProviderException('Date element node is missing.');
     }
     $text = $elem->textContent;
     return $this->parseDate($text, $format);
 }
开发者ID:skoro,项目名称:pod,代码行数:16,代码来源:Provider.php

示例5: parsePodImageUrl

 /**
  * @inheritdoc
  */
 protected function parsePodImageUrl(DOMDocument $document)
 {
     if (!($elem = $document->querySelector('#content_top .primary_photo img'))) {
         throw new ProviderException('Picture is missing');
     }
     $src = $elem->getAttribute('src');
     if (strpos($src, '//') === 0) {
         $src = 'http:' . $src;
     }
     return $src;
 }
开发者ID:skoro,项目名称:pod,代码行数:14,代码来源:NationalGeographic.php

示例6: finalizePod

 /**
  * @inheritdoc
  */
 protected function finalizePod(Pod $pod, DOMDocument $document, $response)
 {
     // Wrapper element for date and base url.
     $element = $document->querySelector('#potd-list-wrap .new-row a.selected');
     if ($element && $element->hasChildNodes()) {
         // Create absolute url from relative.
         $href = parse_url($element->getAttribute('href'));
         if (empty($href['scheme'])) {
             $url = parse_url($this->url);
             $pod->baseUrl = $url['scheme'] . '://' . $url['host'] . $href['path'];
         }
         // Search date element.
         foreach ($element->childNodes as $child) {
             if ($child->nodeType == XML_ELEMENT_NODE && $child->getAttribute('class') == 'date') {
                 $pod->date = $this->parseDate($child->textContent, '%d/%m/%Y');
                 break;
             }
         }
     }
     // Wrapper element for title, image url and description.
     $element = $document->getElementById('potd-wrap');
     if ($element && $element->hasChildNodes()) {
         foreach ($element->childNodes as $child) {
             if ($child->nodeType == XML_ELEMENT_NODE) {
                 switch ($child->tagName) {
                     case 'h1':
                         if (empty($pod->title)) {
                             $pod->title = $child->textContent;
                         }
                         break;
                     case 'div':
                         if ($child->getAttribute('id') == 'image-caption') {
                             $pod->desc = $child->textContent;
                         }
                         break;
                     case 'img':
                         $pod->imageUrl = $child->getAttribute('src');
                         if (($pos = strpos($pod->imageUrl, '?')) !== false) {
                             $pod->imageUrl = substr($pod->imageUrl, 0, $pos);
                         }
                         break;
                 }
             }
         }
     }
 }
开发者ID:skoro,项目名称:pod,代码行数:49,代码来源:Euronews.php


注:本文中的DOMDocument::querySelector方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。