【问题标题】:Get Links with simple html dom使用简单的 html dom 获取链接
【发布时间】:2015-11-11 11:29:19
【问题描述】:

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

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

为此在简单的 html Sun 中使用“用户代理”。

但是我得到了这个错误..

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/>";
}

【问题讨论】:

    标签: php html find file-get-contents simple-html-dom


    【解决方案1】:

    将您的 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,而不是读取返回字符串并将其设置为 $html 的 url,从而覆盖您的 simple_html_dom 实例,因此当您调用 find 方法时,您有一个字符串而不是一个对象。

    【讨论】:

    • 确实如此。我没有想到。非常感谢。
    【解决方案2】:

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

    $html = file_get_html($url);
    

    或使用

    $html = str_get_html($html);
    

    拨打file_get_contents之后。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-27
      • 2023-03-13
      相关资源
      最近更新 更多