【问题标题】:Curl error: Maximum (20) redirects followed卷曲错误:最多 (20) 次重定向
【发布时间】:2017-06-12 19:30:29
【问题描述】:

尝试 CURL 到 myntra 时出现错误。我正在尝试通过 DOMDOCUMENT 获取提取详细信息,但它给出了相同的错误:

最多 (20) 次重定向

这是我的代码:

<?php
        $url = 'http://www.myntra.com/sports-shoes/nike/nike-men-black-dart-12-msl-running-shoes/1547908/buy?src=search&uq=false&q=nike&p=1';
        $ch  = curl_init($url);
        //curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)");
        curl_setopt($ch, CURLOPT_AUTOREFERER, true);
        curl_setopt($ch, CURLOPT_HEADER, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_FAILONERROR, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array("Cookie: test=cookie"));

        $cl  = curl_exec($ch);
        if(curl_exec($ch) === false)
        {
                echo 'Curl error: ' . curl_error($ch);
                echo 'Curl error: ' . curl_errorno($ch);
        }else{
           $dom = new DOMDocument();
           $xpath = new DOMXpath($dom);
           print_r($xpath);            
        }
?>

【问题讨论】:

    标签: php curl web-scraping


    【解决方案1】:

    添加一些 cookie 文件。

    这样

    curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__) . '/cookie.txt');
    curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__) . '/cookie.txt');
    

    它应该工作。

    【讨论】:

    • 确实如此,但不知道为什么
    【解决方案2】:
    <?php
    
    $url = 'http://www.myntra.com/sports-shoes/nike/nike-men-black-dart-12-msl-running-shoes/1547908/buy?src=search&uq=false&q=nike&p=1';
    $ch  = curl_init($url);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:47.0) Gecko/20100101 Firefox/47.0");
    $request_headers = [
                    'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8;',
                    'Accept-Encoding: gzip, deflate',
                    "Connection: keep-alive",
                    "Content-Type: text/html; charset=UTF-8",
    
                ];
    curl_setopt($ch, CURLOPT_AUTOREFERER, true);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FAILONERROR, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
    curl_setopt($ch, CURLOPT_ENCODING, "");
    $cl  = curl_exec($ch);
    $h = curl_getinfo($ch);
    $e = curl_error($ch);
    curl_close($ch);
    var_dump($cl);
    

    有时您必须发送请求标头,然后根据编码进行解码。

    【讨论】:

    • 我现在得到了响应,但是如何从中获取
    • 这是一个完全不同的方面,就像你想如何解析..你想从&lt;script&gt;标签得到什么
    • 你需要研究一下myntra上的代码结构,然后为它写方法..
    • 我正在尝试这样:$dom = new DOMDocument(); $dom->loadHTML($cl); $xpath = 新 DOMXpath($dom); $product_name = $xpath->query('//h1[@class="pdp-title"]');但没有得到值,在萤火虫上看到网络活动时,我收到 500 个内部服务器错误
    【解决方案3】:

    为此使用 CURLOPT_MAXREDIRS 选项

    curl_setopt($ch, CURLOPT_MAXREDIRS , 1000);
    

    我希望它有效,祝你好运!

    【讨论】:

    • 这很少能治愈。循环是问题所在,它不会通过循环更多圈来解决。通常是 cookie 的情况。
    • 在使用 curl_setopt($ch, CURLOPT_MAXREDIRS , 1000); 时,它一直在加载,我还没有结果!!!
    • 删除标题'Accept-Encoding: gzip, deflate',
    • 这不仅不能治愈,而且是个糟糕的建议 (-1)。如果它卡在重定向循环中(它已经在执行默认值 10 左右),那么增加最大重定向只会在再次失败之前循环更多,从而出现针对抓取目标的拒绝服务攻击。 OP 已经不尊重 Robots.txt 协议,这使情况变得更糟。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-21
    • 2014-02-15
    • 1970-01-01
    • 2015-02-16
    • 2016-02-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多