您好,欢迎访问一九零五行业门户网

为什么用curl或file_get_content抓取不到数据解决方案

为什么用curl或file_get_content抓取不到数据。
本帖最后由 xroha 于 2014-12-15 09:49:56 编辑 为什么用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 只是抓取这个页面内容,但这个页面有其它许多的动态内容是不能通过抓取去填充的
------解决思路----------------------
没有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;
其它类似信息

推荐信息