2015-11-11 113 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后。

相关问题