使用简单的 html dom 获取链接

Get Links with simple html dom

我正在尝试从网站获取链接。

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

为此使用 "user-agent" 简单 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/>";
}

$html 是调用 file_get_contents 之后的字符串。尝试

$html = file_get_html($url);

或使用

$html = str_get_html($html);

在调用 file_get_contents 之后。

将您的 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 方法时,你有一个字符串而不是一个对象。