為什麼用curl或file_get_content抓取不到資料。

來源:互聯網
上載者:User
為什麼用curl或file_get_content抓取不到資料。

百度經驗裡,比如http://jingyan.baidu.com/article/00a07f38441c3782d028dc04.html,
直接看頁面原始碼,是有文章資料。
但是用curl ,file_get_content.都無法正常擷取文章內容。
這是為什嗎?已經偽造了IP,來路等,但還是抓取不到。百度是通過什麼防止抓取資料的?

以下是代碼:
function fcontents( $url, $timeout = 5, $referer = "" ){    $ch = curl_init();    $header = array (        'User-Agent: Mozilla/5.0 (Windows NT 5.2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36','X-FORWARDED-FOR:154.125.25.15', 'CLIENT-IP:154.125.25.15'    );    curl_setopt($ch, CURLOPT_URL, $url);    curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);    curl_setopt($ch, CURLOPT_HTTPHEADER, $header); //構造使用者IP    curl_setopt($ch, CURLOPT_REFERER, "http://www.baidu.com/");//構造來路     $result = curl_exec($ch);    curl_close($ch);    return $result;}$html = fcontents('http://jingyan.baidu.com/article/00a07f38441c3782d028dc04.html');echo $html;


回複討論(解決方案)

curl 只是抓取這個頁面內容,但這個頁面有其它許多的動態內容是不能通過抓取去填充的

curl 只是抓取這個頁面內容,但這個頁面有其它許多的動態內容是不能通過抓取去填充的


文章資料應該不是動態吧,我們查看頁面原始碼,應該是能查看到的代碼通過curl都能抓取到吧,而且這個頁面不用登陸也能看到,搜尋引擎的蜘蛛也能抓取,為什麼我現在用curl抓取不到呢?

沒有cookie的原因吧。先把cookie加上。

$url = "http://jingyan.baidu.com/article/00a07f38441c3782d028dc04.html";$cookie_jar = dirname(__FILE__)."/jy.cookie";/* 擷取cookie */$ch = curl_init();curl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar);curl_exec($ch);curl_close($ch);


然後請求的時候帶上cookie:
$ch = curl_init();curl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar);curl_setopt($ch, CURLOPT_HEADER, 0);$res = curl_exec($ch);curl_close($ch);echo $res;

沒有cookie的原因吧。先把cookie加上。

$url = "http://jingyan.baidu.com/article/00a07f38441c3782d028dc04.html";$cookie_jar = dirname(__FILE__)."/jy.cookie";/* 擷取cookie */$ch = curl_init();curl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar);curl_exec($ch);curl_close($ch);


然後請求的時候帶上cookie:
$ch = curl_init();curl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar);curl_setopt($ch, CURLOPT_HEADER, 0);$res = curl_exec($ch);curl_close($ch);echo $res;



加了也不行。在本地環境和三個不同IP的伺服器上試了,都抓不到。


沒有cookie的原因吧。先把cookie加上。

$url = "http://jingyan.baidu.com/article/00a07f38441c3782d028dc04.html";$cookie_jar = dirname(__FILE__)."/jy.cookie";/* 擷取cookie */$ch = curl_init();curl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar);curl_exec($ch);curl_close($ch);


然後請求的時候帶上cookie:
$ch = curl_init();curl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar);curl_setopt($ch, CURLOPT_HEADER, 0);$res = curl_exec($ch);curl_close($ch);echo $res;



加了也不行。在本地環境和三個不同IP的伺服器上試了,都抓不到。



使用我上面的代碼,抓取到的就是百度經驗的頁面。你為何不把你的代碼貼出來(加上cookie的代碼)。



沒有cookie的原因吧。先把cookie加上。

$url = "http://jingyan.baidu.com/article/00a07f38441c3782d028dc04.html";$cookie_jar = dirname(__FILE__)."/jy.cookie";/* 擷取cookie */$ch = curl_init();curl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar);curl_exec($ch);curl_close($ch);


然後請求的時候帶上cookie:
$ch = curl_init();curl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar);curl_setopt($ch, CURLOPT_HEADER, 0);$res = curl_exec($ch);curl_close($ch);echo $res;



加了也不行。在本地環境和三個不同IP的伺服器上試了,都抓不到。



使用我上面的代碼,抓取到的就是百度經驗的頁面。你為何不把你的代碼貼出來(加上cookie的代碼)。



非常感謝,是我自己弄錯了。少加了一行代碼。
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.