所以这是我的 PHP 脚本:
<?php
function scrape(){
$f=fopen("list.txt","r") or exit("Unable to open file!");
while (!feof($f))
{
$site=stream_get_line($f,4096,"\n");
$url="www.majesticseo.com/reports/site-explorer/summary/".$site."?IndexDataSource=F";
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
$regex = '~External Backlinks\s*</p>\s*<p style="font-size: 150%;">\s*<b>(.+?)</b>~';
$result=preg_match($regex,$data,$match);
$link_count=$match[1];
echo($site." ".$link_count);
echo("</br>"); }
}
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, 'www.majesticseo.com/account/login');
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'EmailAddress=myemail@email.com&Password=mypassword123');
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$store=curl_exec($ch);
scrape();
curl_close($ch);
?>
它的问题是scrape()函数和登录部分在单独测试时工作,但是,当我想在登录curl会话中scrape()时,它似乎在没有登录的情况下进行刮擦。我知道这是因为限制已经到达没有登录的抓取网站,并且它不返回任何数据。
为什么会这样?如何让我的脚本在登录时抓取数据?