我正在使用curl 为linkedin 个人资料页面进行网页抓取。如果我们尝试从这个公开的( http://in.linkedin.com/in/ratneshdwivedi ) URL 中提取数据,它就可以工作。当我登录 linkedin 并尝试从此 URL( http://www.linkedin.com/profile/view?id=77597832&locale=en_US&trk=tyah2&trkInfo=tas%3Aravi%20kant%20mishra%2Cidx%3A1-1-1 )获取数据时,它不起作用,而是返回空白数据。
以下是我的源代码:
$html= $this->_getScrapingData ('http://in.linkedin.com/in/ratneshdwivedi',10);
preg_match("/<span class=\"full-name\">(.*)<\/span>/i", $html, $match);
private function _getScrapingData($url,$timeout) {
$ch = curl_init($url); // initialize curl with given url
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]); // set useragent
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // write the response to a variable
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // follow redirects if any
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); // max. seconds to execute
curl_setopt($ch, CURLOPT_FAILONERROR, 1); // stop when it encounters an error
return @curl_exec($ch);
}
提前致谢
最佳答案
您的脚本未使用与您的浏览器相同的 cookie。您需要首先使用脚本完成登录表单。
使用
CURLOPT_COOKIEJAR
CURLOPT_COOKIEFILE
通过您的请求保留 cookie。
关于php - PHP 中的网页抓取 - 可处理某些 URL,但无法处理其他 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19246866/