首页 > 解决方案 > 无法使用 domcrawler 提取 og:image 但可以使用 og:description

问题描述

所以我正在尝试使用 php 包“Goutte”通过 domcrawler 获取一些开放的图形数据。

具体来说,我正在尝试提取og:image.

$goutteClient = new Client();
$guzzleClient = new GuzzleClient(array(
    'timeout' => 15,
));

$goutteClient->setClient($guzzleClient);

try {
    $crawler = $goutteClient->request('GET', $url);
    $crawlerError = false;
} catch (RequestException $e) {
    $crawlerError = true;
}


$OGimage = $crawler->filterXPath("//meta[@name='image']")->attr('content');
//find open graph image
dd($OGimage);

因此,如果使用此 URL 尝试此操作,例如:

https://www.bbc.com/sport/football/49421006

它将返回一个空字符串。

但是,如果我输入meta[@name='description']而不是图像,它将返回

“法国国脚弗兰克·里贝里在夏天离开拜仁慕尼黑后免费转会到意甲球队佛罗伦萨。”

但是当我查看这个页面的来源时,显然有一个og:image标签。

为什么这返回空但描述没有?值得注意的是,所有打开的图形标签都返回带有此 EXCEPT 描述的空字符串,这很好用。

标签: phplaravelguzzlegouttedomcrawler

解决方案


推荐阅读