用随机字符串附加到URL的AJAX请求

Scraping AJAX requests with random strings appended to URL

本文关键字:URL AJAX 请求 随机 字符串      更新时间:2023-09-26

我正试图通过浏览器AJAX请求来监控scorpo/cricket上的板球得分。分析谷歌Chrome中的网络流量,我可以看到我的浏览器正在请求以下格式:

http://www.scorespro.com/cricket/ajax.php?g_sort=league&日期=2014-10-02&mut=1412265716&sut=0&(some_random_number)

当我在谷歌浏览器中点击响应时,我可以看到收到的数据。但是,当我自己尝试请求URL时,没有收到任何数据。为什么会发生这种情况(这与随机字符串有关吗),我该如何绕过它?

是否需要从javascript执行此操作?您是否考虑过通过在您控制的服务器上调用脚本来抽象请求?

例如,在您的服务器上,您可以有一个名为"grabber.PHP"的PHP脚本

<?php
$r = '0.' . rand(1000000000000000, 9000000000000000);
$url = 'http://www.scorespro.com/cricket/ajax.php?g_sort=league&date=2014-10-03&mut=1412328280&sut=0&' . $r;
$useragent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:32.0) Gecko/20100101 Firefox/32.0';
$referer = 'http://www.scorespro.com/cricket/';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_REFERER, $referer);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIEFILE, '/tmp/cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEJAR, '/tmp/cookie.txt');
$response = curl_exec($ch);
curl_close($ch);
$data = array('payload' => $response);
echo json_encode($data);
exit();
?>

然后,您可以通过一个简单的ajax请求调用该页面:

<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript">
$.ajax({
    url: 'http://yourserver.com/grabber.php',
    dataType: 'json',
    type: 'GET',
    success: function(data, textStatus, jqXHR){
        if (data['payload']){
            alert(data['payload']);
        } else {
            alert ('oops');
        }
    }
});

当然,如果你采用这种方法,你必须决定如何从板球网站向抓取器脚本获取你需要请求的URL(即根据你的要求从javascript传递或直接从PHP脚本中获取)