为什么用curl或file_get_content抓取不到数据。

xroha 2014-12-15 09:45:54
为什么用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;
...全文
304 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
xroha 2014-12-15
  • 打赏
  • 举报
回复
引用 3 楼 ohmygirl 的回复:
没有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的服务器上试了,都抓不到。
ohmygirl 2014-12-15
  • 打赏
  • 举报
回复
没有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;
xroha 2014-12-15
  • 打赏
  • 举报
回复
引用 1 楼 whg4585 的回复:
curl 只是抓取这个页面内容,但这个页面有其它许多的动态内容是不能通过抓取去填充的
文章数据应该不是动态的吧,我们查看页面源代码,应该是能查看到的代码通过curl都能抓取到吧,而且这个页面不用登陆也能看到,搜索引擎的蜘蛛也能抓取,为什么我现在用curl抓取不到呢?
小在在 2014-12-15
  • 打赏
  • 举报
回复
curl 只是抓取这个页面内容,但这个页面有其它许多的动态内容是不能通过抓取去填充的
xroha 2014-12-15
  • 打赏
  • 举报
回复
引用 5 楼 ohmygirl 的回复:
[quote=引用 4 楼 xroha 的回复:] [quote=引用 3 楼 ohmygirl 的回复:] 没有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的服务器上试了,都抓不到。[/quote] 使用我上面的代码,抓取到的就是百度经验的页面。你为何不把你的代码贴出来(加上cookie的代码)。[/quote] 非常感谢,是我自己弄错了。少加了一行代码。
ohmygirl 2014-12-15
  • 打赏
  • 举报
回复
引用 4 楼 xroha 的回复:
[quote=引用 3 楼 ohmygirl 的回复:] 没有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的服务器上试了,都抓不到。[/quote] 使用我上面的代码,抓取到的就是百度经验的页面。你为何不把你的代码贴出来(加上cookie的代码)。

21,887

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧