使用PHP抓取Amazon.com网页

时间:2019-09-10 00:33:34

标签: php curl domdocument

我正在尝试简单地获取远程Amazon URL的html。我有工作代码,但也许他们做了些改变?不确定。我已经花了数小时从这里到那里尝试代码示例和插件,但是没有任何效果。这是我现在所拥有的,但是当然也不起作用:

    $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $item['URL']);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
        $output = json_decode(curl_exec($curl));
            //echo curl_getinfo($curl, CURLINFO_HTTP_CODE);
        curl_close($curl);
        @file_put_contents($graphics_file_root.'rps/amazon/temp2.html',$output);

        $html = new DOMDocument();
        @$html->loadHTML($output);
        @file_put_contents($graphics_file_root.'rps/amazon/temp.html',$html->saveHTML());
        $temp = $html->getElementsByTagName('img');

        $html = file_get_contents($item['URL']);
        @file_put_contents($graphics_file_root.'rps/amazon/temp2.html',$html);
        $temp = $html->getElementsByTagName('img');
        echo count($temp);
        print_r($temp);

这不起作用。 simple_html_dom不起作用。我什么也找不到。

1 个答案:

答案 0 :(得分:0)

我在网上发现的一些代码似乎是json特定的,并删除了json-decode对其进行了修复:

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $item['URL']);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
        $output = curl_exec($curl);
            //echo curl_getinfo($curl, CURLINFO_HTTP_CODE);
        curl_close($curl);
        //file_put_contents($graphics_file_root.'rps/amazon/temp2.html',$output);

        $html = new DOMDocument();
        @$html->loadHTML($output);
        //file_put_contents($graphics_file_root.'rps/amazon/temp.html',$html->saveHTML());

        $temp = $html->getElementsByTagName('img');