2012-07-24 157 views
2

我正在使用簡單的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/

+0

確定'$ remote_html'包含關鍵字? – 2012-07-24 21:01:41

+0

我確信$ remote_html包含關鍵字。 – Muggles 2012-07-24 21:06:29

+0

嘗試'echo $ html-> find(「meta [keywords]」);' – 2012-07-24 21:08:31

回答

1

給這一個鏡頭:

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

編輯:

這可能爲你工作更好ř情況http://php.net/manual/en/function.get-meta-tags.php

+0

仍產生一個空數組。 – Muggles 2012-07-24 21:18:45

+0

嗯。不確定。我真的沒有太多時間來解決問題,但是您可能想要查看我上面的編輯... – 2012-07-24 21:39:14

+0

我更想使用Simple HTML Dom庫,所以我有更多的自定義請求來發送遠程頁面。 – Muggles 2012-07-26 19:34:15

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'); 
10

查找()不返回一個對象,但包含的陣列(在這種情況下)1個對象。另外'關鍵字'不是一個屬性,但'名稱'是。用途:

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

試試這個

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