繁体   English   中英

Symfony 的 DomCrawler 如何从 html 源中提取元描述

[英]Symfony's DomCrawler how can I extract meta description from html source

使用 Symfony 的 DomCrawler 如何从 html 源中提取元描述? http://symfony.com/doc/current/components/dom_crawler.html

$crawler = new Crawler();
$crawler->addHtmlContent($html->content, 'UTF-8');

$title = $crawler->filter('title')->text();

MSN 元描述示例

<meta name="description" content="The new MSN, Your customizable collection of the best in news, sports, entertainment, money, weather, travel, health, and lifestyle, combined with Outlook, Facebook, Twitter, Skype, and more."/>

我假设您正在尝试获取内容属性值,因此请尝试使用

$data = $crawler->filterXpath("//meta[@name='description']")->extract(array('content'));

并循环$data

$meta_description = $crawler->filter('meta[name="description"]')->eq(0)->attr('content');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM