【发布时间】:2014-12-22 18:23:51
【问题描述】:
问题:
我有一个 php 抓取功能和代码都运行良好,但是它超时,因为它试图加载 60 个不同的页面......
我正在考虑使用 AJAX 在循环中一次加载一个页面。由于我对 AJAX 很陌生,我遇到了一些麻烦。
这是我到目前为止所拥有的,如果我提供它们,我可以让它循环遍历链接,但是我希望它抓取第 1 页,返回下一页链接,然后连续循环抓取下一页,直到没有更多页面了。就目前而言,它进入了无限循环模式......
各位有什么想法吗?
这是我从一个使用数组的 youtube 视频中获取的代码(我只通过一个字符串)
<?php
ini_set('display_errors',1);
//error_reporting(E_ALL);
set_time_limit(0);
require_once 'scrape_intrepid.php';
//posted to this page
if(isset($_POST['id'])) {
//get the id
$id = $_POST['id'];
//this returns the next page link successfully, i just cant get it back into the function
$ids = scrapeSite($id);
echo $ids;
echo "<br>";
$data = $id . " - DONE";
echo json_encode($data);
exit();
} else {
$ids = 'http://www.intrepidtravel.com/search/trip?page=1';
}
?>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(function() {
function update() {
ids = <?=json_encode($ids);?>;
if(ids){
var id = ids;
$.post("index.php",{id:id}).done(function(msg){
console.log(ids,msg);
update();
});
} else {
console.log("done");
$("#log").html("Completed!");
}
}
$("#go").click(function() {
$("#go").html("Loading...");
update();
});
});
</script>
</head>
<body>
<button id="go">Go button</button>
<div id="log">Results</div>
</body>
【问题讨论】:
标签: javascript php jquery html ajax