使用Simple HTML Dom检索关键字元标记内容?

时间:2012-07-24 20:52:06

标签: php screen-scraping simple-html-dom

我正在使用Simple HTML Dom从远程网页上删除关键字,但我无法弄清楚如何实现这一点。

我目前正在使用以下代码。

$html = str_get_html($remote_html);
echo $html->find("meta[keywords]")->content;

并收到以下错误:

Trying to get property of non-object

http://simplehtmldom.sourceforge.net/

4 个答案:

答案 0 :(得分:11)

find()不返回对象,而是返回包含(在本例中)1个对象的数组。 “关键字”也不是属性,但“名称”是。使用:

$oHTML = str_get_html( $remote_html );
$arElements = $oHTML->find( "meta[name=keywords]" );
echo $arElements[0]->content;

答案 1 :(得分:3)

$headers = array();
$headers["title"] = $html-> find("title",0)-> plaintext;
$headers["keywords"] = $html-> find("meta[name=keywords]",0) ->getAttribute('content');  
$headers["description"] = $html-> find("meta[name=description]",0) ->getAttribute('content'); 

答案 2 :(得分:2)

试一试:

$html->find('meta[description]');

编辑:

这可能更适合您的情况http://php.net/manual/en/function.get-meta-tags.php

答案 3 :(得分:2)

试试这个

$Inner_anchor = file_get_html("Your-Url");
$Inner_anchor->find("head meta[name='description']", 0)->content;