从只有登录用户才能访问的页面抓取HTML


Grabbing HTML from a page only a logged in user can access?

基本上我想从一个只有登录用户才能看到的网页上抓取一些数据,即Facebook分析和页面见解。

如果我查询特定的页面并试图正常抓取它,它不起作用。

有什么办法可以做到吗?

使用CURL和cookie会话远程登录页面

使用这些选项:

$url = 'https://somewhere.com';
$post['user'] = 'myuser';
$post['pass'] = 'mypass';
$ch = curl_init( );
curl_setopt($ch, CURLOPT_URL, $url );
curl_setopt($ch, CURLOPT_COOKIESESSION, true );
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt' );
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt' );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);    
curl_setopt($ch, CURLOPT_POST, count( $post ) );
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query( $post ) );
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; Linux x86_64)     AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/32.0.1700.107 Chrome/32.0.1700.107 Safari/537.36');
echo $result = curl_exec($ch); // Outputs HTML response from url

*请注意,帖子的名称与您试图登录的网站不同。

我已经成功使用过了。

$context = stream_context_create(array(
    'http' => array('header'  => "Authorization: Basic " . base64_encode("$username:$password"))
));
$data = file_get_contents($url, false, $context);