為什麼用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的代碼)。
非常感謝,是我自己弄錯了。少加了一行代碼。