php - PHP 中的网页抓取 - 可处理某些 URL,但无法处理其他 URL

标签 php web-scraping webharvest

我正在使用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/

相关文章:

java - 如何剥离从网络收获中获得的文本的一部分

php/ajax REMOTE_ADDR 设置为伪造网络适配器的 IP

python - 在 Beautiful Soup 中使用 css 选择器获取正确的节点?

java - 网页抓取 Java 初学者

python-2.7 - 从脚本中抓取。不会导出数据

python - 如何使用python在HTML中访问/设置 'select'标签

java - 有哪些好的 Java 库可以从网页中搜索和抓取数据。

php - 我如何在 Doctrine 2 DQL 中使用 DATE()?

php - 将字段从一个数据库复制到 MySQL 中的另一个数据库

php - 清除数组中的重复项