【问题标题】:How to use ajax to scrape one page at a time, return the next page link and go again如何使用ajax一次刮一页,返回下一页链接再去
【发布时间】: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


    【解决方案1】:

    最终以另一种方式解决了这个问题:我调用 function.php 的函数运行脚本并返回下一个要抓取的 URL。这是 msg 值,因此一旦验证,就会再次调用刷新。刚刚处理了 60 页,每页耗时 38 秒:S

    <script>
    $(document).ready(function() {
    
        refresh('http://www.intrepidtravel.com/search/trip?');
    
        function refresh(url) {
            $.ajax({
                type: "GET",
                url: "function.php",
                data: 'url=' + url,
                success: function(msg){
                    $('#result').append('--->Completed! <br>Next Page: is ' + msg);
                    console.log(msg);
                    if ($.trim(msg) == 'lastpage'){
                        $('#result').append('--->Last page - DONE!');
                    }
                    else {
                        refresh(msg);
                    }
                }
    
            }); // Ajax Call
        } //refresh
    
    }); //document.ready
    </script>
    

    还有function.php文件:

    require_once 'scrape_intrepid.php';

    if ($_GET['url']){
        $url = $_GET['url'];
        if ($url=="lastpage"){
            echo $url;
        } else {
        $nextlink = scrapeSite($url);
        echo($nextlink);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-02
      • 2022-06-11
      • 1970-01-01
      • 2016-08-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多