【问题标题】:Scrape Amazon.com webpage with PHP使用 PHP 抓取 Amazon.com 网页
【发布时间】:2019-09-10 00:33:34
【问题描述】:

我正在尝试简单地获取远程 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 不起作用。我找不到任何东西。

【问题讨论】:

  • 你在这里并没有真正给我们太多。你有错误吗?你要什么网址?
  • 定义不起作用
  • BTW suppress operator @ 通常不是首选,至少在像您这样的调试情况下,您需要显示所有错误。
  • 尝试从亚马逊获取产品页面的任何或所有 URL。一般形式为amazon.com/dp/THEPRODUCTCODE。在我设置跟随位置之前,我收到了 301 移动错误。返回的页面在某些情况下看起来像二进制,而在其他情况下看起来像一个空页面。

标签: php curl domdocument


【解决方案1】:

看起来我在网上找到的一些代码是特定于 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');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-23
    • 1970-01-01
    • 1970-01-01
    • 2018-10-17
    • 2014-11-01
    相关资源
    最近更新 更多