【发布时间】:2016-08-30 03:15:35
【问题描述】:
我们有一个内置 PHP 的爬虫,可以从客户的页面中提取重要信息。问题是我们的大多数客户发布自定义缩短链接,使用 302 去最终目的地。直到这个最新的客户端,我们的爬虫已经成功地跟踪了这些(见下面的代码)。这是一个示例链接:
http://www.dose.com/lists/26235/s
如果您在浏览器中访问,您会看到标准的 302 行为,但如果您使用爬虫访问它,它只会返回 200 并且不会重定向。这使我相信我必须使请求看起来尽可能“自然”,但我仍然没有取得任何成功。最后,这是我们代码的 cURL 部分:
function sendRequest($url)
{
global $ch;
$user_agent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5)".
" Gecko/20041107 Firefox/1.0";
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Language: en-US,en;q=0.8',
'Connection: keep-alive'
));
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent );
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_ENCODING, '');
$contents = curl_exec($ch);
//curl_close($ch);
return $contents;
}
已编辑以包含下面的建议,尽管问题仍然存在。
【问题讨论】:
-
dose.com/lists/26235/s - 返回“301 永久移动”
-
你是对的。感谢您的更正。仍然不太清楚为什么 cURL 会遇到这样的困难。
标签: php redirect curl web-crawler