0

我试图在通过 curl 登录网站后从网站上抓取账单列表,但其中一个页面上的内容与我的浏览器中的内容不同(即,它不显示账单列表,而是显示“您的账单历史无法显示”)。我可以正确抓取仅在登录后才可用的其他页面,所以我很困惑为什么当我使用 curl 时该页面拒绝显示账单历史记录。

这是我的代码:

//Load login page
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.domain.com/login');
curl_setopt($ch, CURLOPT_REFERER, 'https://www.domain.com');
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; rv:20.0) Gecko/20100101 Firefox/20.0');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieLocation);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieLocation);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$webpage = curl_exec($ch);

//Submit post to login page to authentify
$postVariables = 'emailAddress='.$username.
    '&password='.$password;
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postVariables);
curl_setopt($ch, CURLOPT_URL, 'https://www.domain.com/login/POST.servlet');
curl_setopt($ch, CURLOPT_REFERER, 'https://www.domain.com/login');
$webpage = curl_exec($ch);

//Go to my account main page now that we are logged in
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_URL, 'https://www.domain.com/My_Account');
curl_setopt($ch, CURLOPT_REFERER, $target);
$webpage = curl_exec($ch); //shows the same content as in the browser
$accountNumber = return_between($webpage, 'id="accountID1">', '<', EXCL); //this is correctly found

//Go to bills page
curl_setopt($ch, CURLOPT_URL, 'https://www.domain.com/Bill_History/?accountnumber='.$accountNumber);
curl_setopt($ch, CURLOPT_REFERER, 'https://www.domain.com/My_Account');
$webpage = curl_exec($ch); //Not showing the same content as in the browser

最后一个 curl_exec 不能正常工作。

我已经广泛检查了页面的逻辑并使用 Tamper Data 来分析发生了什么:似乎没有任何 javascript / ajax 调用可以单独提取账单历史记录,也没有 POST 请求:据我所知查看账单历史记录应在页面加载时显示。

关于我可以尝试解决什么或可能是什么问题的任何想法?它在其他页面上工作的事实尤其令人费解。

提前致谢!

编辑:它仍然不起作用,但我在他们的网站上找到了另一个页面,在那里我可以获得我需要的内容以及正确显示内容的位置 - 所以不再需要解决方案。

4

1 回答 1

0

您可以添加“真实”浏览器通常传输的附加标头字段:

$header[] = 'Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5';
$header[] = 'Connection: keep-alive';
$header[] = 'Keep-Alive: 300';
$header[] = 'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7';
$header[] = 'Accept-Language: en-us,en;q=0.5';

仅举几个。

如果您碰巧使用 FFox,请获取方便的“实时 HTTP 标头”插件并检查您的浏览器在加载相关页面时传输的标头。然后尝试做同样的事情。

于 2013-10-15T18:24:42.323 回答