使用SimpleHTMLDom检索关键字元标记内容


Retrieve keywords meta tag content with 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/

find()返回的不是一个对象,而是一个包含(在本例中)1个对象的数组。此外,"keywords"不是属性,但"name"是。使用:

$oHTML = str_get_html( $remote_html );
$arElements = $oHTML->find( "meta[name=keywords]" );
echo $arElements[0]->content;
$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'); 

尝试一下:

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

编辑:

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

试试这个

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