2015-11-11 161 views
3

我正在嘗試從該網站獲取鏈接。用簡單的html dom獲取鏈接

http://www.perfumesclub.com/es/perfume/mujer/c/

對於此用途 「用戶代理」,在簡單的HTML太陽

但我得到這個錯誤..

Fatal error: Call to a member function find() on string in C:\Users\Desktop\www\funciones.php on line 448 

這是我的代碼:

謝謝^^

$url = 'http://www.perfumesclub.com/es/perfume/mujer/c/'; 

$option = array(
     'http' => array(
      'method' => 'GET', 
      'header' => 'User-Agent: Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)', 
     ) 
); 
$context = stream_context_create($option); 
$html = new simple_html_dom(); 
$html = file_get_contents ($url, false, $context); 


$perfumes = $html->find('.imageProductDouble'); --> this is line 448 

foreach($perfumes as $perfume) { 
      // Get the link 

      $enlaces = "http://www.perfumesclub.com" . $perfume->href; 
      echo $enlaces . "<br/>"; 
} 

回答

2

包裝您的file_get_contents在str_get_html功能

// method 1 
$html = new simple_html_dom(); 
$html->load(file_get_contents ($url, false, $context)); 
// or method 2 
$html = str_get_html(file_get_contents ($url, false, $context)); 

你正在創建一個新的DOM,並將其分配給變量$ HTML,比讀取URL返回的字符串,並將其設置爲$ HTML,從而覆蓋您的simple_html_dom實例,所以當你調用find方法時你有一個字符串而不是一個對象。

+0

確實如此。我沒有想到過我。非常感謝。 – Thane

2

$html是調用file_get_contents後的字符串。嘗試

$html = file_get_html($url); 

或使用

$html = str_get_html($html); 

調用file_get_contents後。

相關問題