php的curl正常抓取页面程序如下:
$url = 'http://www.baidu.com'; $ch = curl_init(); curl_setopt($ch, curlopt_url, $url); curl_setopt($ch, curlopt_verbose, true); curl_setopt($ch, curlopt_header, true); curl_setopt($ch, curlopt_nobody, true); curl_setopt($ch, curlopt_customrequest, 'get'); curl_setopt($ch, curlopt_returntransfer, true); curl_setopt($ch, curlopt_timeout, 20); curl_setopt($ch, curlopt_autoreferer, true); curl_setopt($ch, curlopt_followlocation, true); $ret = curl_exec($ch); $info = curl_getinfo($ch); curl_close($ch);
如果你抓取到的是302状态,是因为再抓取的过程中,有的跳转需要给下一个链接传递参数,而下一个链接同时也设置了如果没接收到相应的参数是为非法访问。
curl_setopt($curl, curlopt_customrequest, 'get');
显示就应该正常了。
上面用来抓取功能,几乎应该没问题的。你可以查一下curlopt_customrequest相关资料。
使用一个自定义的请求信息来代替”get”或”head”作为http请求。这对于执行”delete” 或者其他更隐蔽的http请求。有效值如”get”,”post”,”connect”等等。也就是说,不要在这里输入整个http请求。例如输入”get / http/1.0\r\n\r\n”是不正确的。
如对本文有疑问, 点击进行留言回复!!
如何去设计前端框架能力?星巴克消息开放项目从0到1,从点到面的思考
PHP制作日历,实现在1970年到2020年之间的任何一个月份的输出。上半部分
网友评论