【问题标题】:Get SimpleXMLElement from Meta description [duplicate]从元描述中获取 SimpleXMLElement [重复]
【发布时间】:2014-09-03 13:25:03
【问题描述】:

我正在尝试检索包含在 SimpleXMLElement 中的一些元数据。我正在使用 XPATH,但我很难获得我感兴趣的价值。

这是网页标题的摘录(来自:http://www.wayfair.de/CleverFurn-Couchtisch-Abby-69318X2-MFE2223.html

你知道我如何检索包含以下内容的数组中的所有 xmlns 数据:

1) og:类型 2)OG:网址 3) OG:图像 …… x) og:upc


<meta xmlns:og="http://opengraphprotocol.org/schema/" property="og:title" content="CleverFurn Couchtisch &quot;Abby&quot;" />


这是我的 php 代码

<?php
$html = file_get_contents("http://www.wayfair.de/CleverFurn-Couchtisch-Abby-69318X2-MFE2223.html");
$doc = new DOMDocument();
$doc->strictErrorChecking = false;
$doc->recover=true;
@$doc->loadHTML("<html><body>".$html."</body></html>");

$xpath = new DOMXpath($doc);
$elements = $xpath->query("//*/meta[@property='og:url']");

if (!is_null($elements)) {
foreach ($elements as $element) {
echo "<br/>[". $element->nodeName. "]";
var_dump($element);
  $nodes = $element->childNodes;
  foreach ($nodes as $node) {
     echo $node->nodeValue. "\n";
     }
   }
 }
?>

【问题讨论】:

    标签: php html xml xpath simplexml


    【解决方案1】:

    刚刚找到答案:

    How to get Open Graph Protocol of a webpage by php?

    <?php
    $html = file_get_contents("http://www.wayfair.de/CleverFurn-Couchtisch-Abby-69318X2-MFE2223.html");
    libxml_use_internal_errors(true); // Yeah if you are so worried about using @ with warnings
    $doc = new DomDocument();
    $doc->loadHTML($html);
    $xpath = new DOMXPath($doc);
    $query = '//*/meta[starts-with(@property, \'og:\')]';
    $metas = $xpath->query($query);
    foreach ($metas as $meta) {
        $property = $meta->getAttribute('property');
        $content = $meta->getAttribute('content');
        $rmetas[$property] = $content;
    }
    var_dump($rmetas);
    ?>
    

    【讨论】:

      猜你喜欢
      • 2011-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多