phantomJS - 刮掉表内的内容

时间:2016-11-14 23:16:15

标签: javascript phantomjs scrape

我正在使用phantomJS刮一页。该脚本由我在最后添加的php函数调用。我也添加了HTML代码的模型。

脚本等待加载类main-table-redesign的表。

在div加载表之后,我需要获取该特定表中的内容。我不知道该怎么做这部分。我可以通过page.content获取整个页面的内容,但我认为这大大增加了完成操作的时间。

function waitFor(testFx, onReady, timeOutMillis) {
    var maxtimeOutMillis = timeOutMillis ? timeOutMillis : 3000, //< Default Max Timout is 3s
        start = new Date().getTime(),
        condition = false,
        interval = setInterval(function() {
            if ( (new Date().getTime() - start < maxtimeOutMillis) && !condition ) {
                // If not time-out yet and condition not yet fulfilled
                condition = (typeof(testFx) === "string" ? eval(testFx) : testFx()); //< defensive code
            } else {
                if(!condition) {
                    // If condition still not fulfilled (timeout but condition is 'false')
                    console.log("'waitFor()' timeout");
                    phantom.exit(1);
                } else {
                    // Condition fulfilled (timeout and/or condition is 'true')
                    console.log("'waitFor()' finished in " + (new Date().getTime() - start) + "ms.");
                    typeof(onReady) === "string" ? eval(onReady) : onReady(); //< Do what it's supposed to do once the condition is fulfilled
                    clearInterval(interval); //< Stop this interval
                }
            }
        }, 250); //< repeat check every 250ms
};

//------------------------------------------------------------------------------


var page = require('webpage').create();

page.open(pageURL, function (status) {
    // Check for page load success
    if (status !== "success") {
        console.log("Unable to access network");
    } else {    
    
        waitFor(function() {
            // Check in the page if a specific element is now visible
            return page.evaluate(function() {
                return !!document.querySelector(".main-table-redesign");
            });
        }, function() {

            //TODO
            //return table.main-table.redesign content
            
           phantom.exit();
        });
    }
    
});

我在todo部分尝试了这个,只是为了看看我是否可以获得文档标题:

        var title = page.evaluate(function() {
            return document.title;
        }
        console.log(title);

但是当我这样做时,脚本就会继续运行。我知道这是因为评估沙盒。那么我怎样才能返回表格的内容(带标签)?

下面是调用脚本的PHP代码,虽然这里不需要。

<?php

$content = execute("waitfor.js");

var_dump($content);


function execute($script, $args = array(), $options = array(), $bin = 'phantomjs', $debug = true) {

    $option_str = '';
    foreach ($options as $option => $value)
    {
        $option_str .= '--'.$option.'='.$value.' ';
    }

    // Escape
    $cmd = escapeshellcmd("{$bin} {$option_str}{$script} " . implode(' ', $args));
    if($debug) $cmd .= ' 2>&1';
    // Execute
    $result = shell_exec($cmd);
    if($debug) return $result;
    if($result === null) return false;

    // Return
    if(substr($result, 0, 1) !== '{') return $result; // not JSON
    $json = json_decode($result, $as_array = true);
    if($json === null) return false;
    return $json;

}

HTML代码非常庞大,甚至是我需要删除的部分。所以我只想添加它的样子。 HTML模型:

<body>
    <div class='table-content'>
        <table class="main-table-redesign" id="main-table" border="0" width="100%" cellpadding="0" cellspacing="0">
            <tr class="table-data-row>
                <td class="table-data-cell-bar">A1</td>
                <td class="table-data-cell-bar">A2</td>
            </tr>
            <tr class="table-data-row>
                <td class="table-data-cell-bar">B1</td>
                <td class="table-data-cell-bar">B2</td>
            </tr>
        </table>
    </div>
</body>

真的很感激,如果有人能够澄清为什么这样做不起作用以及我能做些什么来使它发挥作用。

0 个答案:

没有答案