2016-12-12 45 views
0

以下是我的代码,我试图抓取以下URL,但由于某些原因,html源代码根本没有被刮掉。为什么在这个URL上不会发生刮擦?无法刮取Zazzle产品网址

我试图使用File_get_contents以及简单的HTML DOM库,但它没有刮。

URL: http://www.zazzle.com/protoceratops_t_shirt-235065458404753105 

function get_data($url) { 
    $ch = curl_init(); 
    $timeout = 5; 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); 
    $data = curl_exec($ch); 
    curl_close($ch); 
    return $data; 
} 

echo get_data('http://www.zazzle.com/protoceratops_t_shirt-235065458404753105'); 
+0

您是否收到错误?该代码是否仅为'http:// www.google.com /'返回任何内容? – castis

回答

0

你可以试试这个:

function get_data($url) { 
    try { 
     $ch = curl_init(); 

     $timeout = 5; 

     if (FALSE === $ch) 
      throw new Exception('failed to initialize'); 

     curl_setopt($ch, CURLOPT_URL, $url); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
     curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); 

     $content = curl_exec($ch); 

     if (FALSE === $content) 
      throw new Exception(curl_error($ch), curl_errno($ch)); 
     // ...process $content now 
     return $content; 

    } catch(Exception $e) { 

     trigger_error(sprintf(
      'Curl failed with error #%d: %s', 
      $e->getCode(), $e->getMessage()), 
      E_USER_ERROR); 
    } 
} 

echo get_data('http://www.zazzle.com/protoceratops_t_shirt-235065458404753105'); 

这也将返回错误,如果你碰巧有任何。

一切归功于: curl_exec() always returns false