2016-12-12 107 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