使用DOM(getElementsByTagName)获取“内容”元标记

时间:2023-02-04 20:21:51

Is it possible to get meta tags content using DOM (getElementsByTagName) for example

例如,是否可以使用DOM(getElementsByTagName)获取元标记内容

This the meta tag that i'm tring to get the content from.

这是我要从中获取内容的元标记。

<span class="nobr">
    <a href="/title/tt1981115/releaseinfo?ref_=tt_ov_inf " title="See all release dates"> 
        8 November 2013
        <meta itemprop="datePublished" content="2013-11-08">
        (USA)
    </a>            
</span>

And this is how i'm tring to get the meta content.

这就是我想要获取元内容的方式。

$metas = $dom->getElementsByTagName('meta');
for($i=0; $i <$metas-> length; $i++){
    $itemprop = $metas->item($i)->getAttribute("itemprop");
    if ($itemprop == "datePublished"){
        if ($metas->item($i)->textContent!=''){
            $res['published'] = $metas->item($i)->textContent;
        }
    }
}

Can someone tell me why won't it get the content?

有人能告诉我为什么不能获得内容?

1 个解决方案

#1


0  

Maybe you need value of content attribute?

也许你需要内容属性的价值?

$metas = $dom->getElementsByTagName('meta');
for($i=0; $i <$metas-> length; $i++){
    $itemprop = $metas->item($i)->getAttribute("itemprop");
    $content = $metas->item($i)->getAttribute("content");
    if ($itemprop == "datePublished" && $content !== ''){
        $res['published'] = $content;
    }
}

var_dump($res);

Be careful, this code valid for the html example that shows in question. Just you can see direction of your code

请注意,此代码对于显示问题的html示例有效。只是你可以看到代码的方向

#1


0  

Maybe you need value of content attribute?

也许你需要内容属性的价值?

$metas = $dom->getElementsByTagName('meta');
for($i=0; $i <$metas-> length; $i++){
    $itemprop = $metas->item($i)->getAttribute("itemprop");
    $content = $metas->item($i)->getAttribute("content");
    if ($itemprop == "datePublished" && $content !== ''){
        $res['published'] = $content;
    }
}

var_dump($res);

Be careful, this code valid for the html example that shows in question. Just you can see direction of your code

请注意,此代码对于显示问题的html示例有效。只是你可以看到代码的方向