从特定链接获取标题

问题描述 投票:0回答:1

我正在尝试从 anilink URL 获取标题。这个特定的代码适用于 MyAnimeList 网站,但是在 AniList 网站上,它不断返回“AniList”,这是该网站,我相信该网站在使用 jQuery 加载网页后正在更新元标记,但是 Facebook 和 Discord 等网站可以获得系列的标题。然而我的代码不能。

这是我正在使用的代码。 例如,这是来自 anist 网站的随机 URL

https://anilist.co/anime/527/Pocket-Monsters/

myfunction(https://anilist.co/anime/527/Pocket-Monsters/)

function myfunction($form_value)
{

$html = file_get_contents_curl($form_value);
 
       //parsing begins here:
         $doc = new DOMDocument();
         @$doc->loadHTML($html);
         $nodes = $doc->getElementsByTagName('title');
   
         //get and display what you need:
         $title = $nodes->item(0)->nodeValue;
        
         $metas = $doc->getElementsByTagName('meta');
         
        for ($i = 0; $i < $metas->length; $i++)
        {
            $meta = $metas->item($i);
            if($meta->getAttribute('property') == 'og:title')
               {$title = $meta->getAttribute('content');}
           if($meta->getAttribute('property') == 'og:site_name')
              $site_name = $meta->getAttribute('content');
       }


 return $title;
 
 }

它回来了。

AniList 

这是元标记。

<meta property="og:title" content="Pokémon" data-vue-meta="true">

所以我期待它回归

Pokémon

我应该使用其他网站来获得所需的结果吗?

php meta-tags
1个回答
0
投票

Anilist
是页面标记中给出的标题。如果您在浏览器中看到任何其他内容,请检查应用程序是否使用 Javascript 覆盖了标题。如果是这种情况,纯 PHP 方法将无助于读取页面的最终标题。您要么需要在浏览器中运行整个页面并从那里读取输出,要么使用适当的 API

© www.soinside.com 2019 - 2024. All rights reserved.