2016-01-20 59 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