curl抓取页面时遇到重定向的解决方法

用php的curl抓取网页遇到了问题,为阐述方便,将代码简化如下:

<?php
function curlGet($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_HEADER, true);
return curl_exec($ch);
}

$url = 'http://144go.com';
echo curlGet($url), "\n";

代码的目的很简单,抓取页面:http://www.144go.com
执行上述代码,得到的结果:

HTTP/1.1 301 Moved Permanently
Content-Length: 144
Content-Type: text/html
Location: http://www.144go.com/
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
Date: Mon, 03 Sep 2012 04:25:22 GMT
 
<head><title>Document Moved</title></head>
<body><h1>Object Moved</h1>This document may be found <a HREF="http://www.144go.com/">here</a></body>


由结果中的
Location: http://www.144go.com/
可知http://144go.com被重定向到了http://www.144go.com/
怎么办呢,要用正则分析出Location部分的链接,重复执行执行curlGet吗?行到是行,就是有点麻烦。

其实只要加一条语就可以了:

<?php
function curlGet($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_HEADER, true);
  //函数中加入下面这条语句
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
return curl_exec($ch);
}

再次执行代码,可以抓取到想要的页面。
CURLOPT_FOLLOWLOCATION指明:
让curl递归的抓取http头中Location中指明的url。
当抓取次数超过CURLOPT_MAXREDIRS时,递归将终止。
在抓取中任何跳转带来的问题,都可通过设置此参数解决。


有关重定向的问题,可参考HTTP返回码中301与302的区别
---------------------

原文链接:https://blog.csdn.net/qmhball/article/details/7937534

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
使用PHP的curl库可以方便地实现网页抓取功能。通过curl库,我们可以发送HTTP请求并获取服务器响应的内容。以下是一个使用php curl抓取页面所有链接的方法: 1. 创建一个curl资源句柄: ``` $ch = curl_init(); ``` 2. 设置curl选项,包括目标URL、请求头信息和其他参数: ``` curl_setopt($ch, CURLOPT_URL, "目标URL"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (iPhone; CPU iPhone OS 8_0 like Mac OS X) AppleWebKit/600.1.3 (KHTML, like Gecko) Version/8.0 Mobile/12A4345d Safari/600.1.4"); ``` 其中,`CURLOPT_URL`用于设置目标URL,`CURLOPT_RETURNTRANSFER`用于设置是否将抓取的内容作为字符串返回,`CURLOPT_FOLLOWLOCATION`用于设置是否跟随重定向,`CURLOPT_USERAGENT`用于设置用户代理。 3. 执行curl请求并获取响应内容: ``` $response = curl_exec($ch); ``` 4. 使用正则表达式或其他方法从响应内容中提取所有链接: ``` preg_match_all('/<a\s+href=["\'](.*?)["\']/', $response, $matches); $links = $matches<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [php curl抓取网页的介绍和推广及使用CURL抓取淘宝页面集成方法](https://download.csdn.net/download/weixin_38594687/13020038)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [PHP curl实现抓取302跳转后页面的示例](https://download.csdn.net/download/weixin_38500572/13045232)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [php中curl抓取页面](https://blog.csdn.net/weixin_27727467/article/details/115831006)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lxw1844912514

你的打赏就是对我最大的鼓励

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值