2016-01-20 38 views
3

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

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

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

例MSN meta描述

<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."/> 

回答

7

我假设你正在试图获取内容属性值,所以尽量使用

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

和环通$data