【问题标题】:CasperJS can not trigger twitter infinite scrollCasperJS 无法触发推特无限滚动
【发布时间】:2015-08-21 07:51:06
【问题描述】:

我正在尝试使用 CasperJS 从 twitter 获取一些信息。我被无限滚动卡住了。问题是,即使使用 jquery 向下滚动页面也没什么用。既不滚动,也不触发window 上的确切事件(像 uiNearTheBottom 之类的东西)似乎无济于事。 有趣的事情——在 FF 和 Chrome 中通过 js 控制台注入 JS 代码时,所有这些尝试都有效。 这是示例代码:

casper.thenEvaluate(function(){
    $(window).trigger('uiNearTheBottom');
});

casper.thenEvaluate(function(){
    document.body.scrollTop  =  document.body.scrollHeight;
});

【问题讨论】:

  • 当 CasperJS 将 jQuery 注入客户端页面时,它会阻止 Twitter 无限滚动加载的内容。这是一个特定于站点的问题。请在下面查看我的答案以获取解决方案。

标签: phantomjs infinite-scroll casperjs


【解决方案1】:

如果 casper.scrollToBottom() 失败或 casper.scroll_to_bottom() 失败,那么下面的将为你服务:

this.page.scrollPosition = { 顶部:this.page.scrollPosition["top"] + document.body.scrollHeight,左:0 };

一个工作示例:

casper.start(url, function () {
 this.wait(10000, function () {
    this.page.scrollPosition = { top: this.page.scrollPosition["top"] + document.body.scrollHeight, left: 0 };
    if (this.visible("div.load-more")) {
        this.echo("I am here");
    }
})});

它使用找到的底层 PhantomJS 滚动 here

【讨论】:

  • 您确定 document.body.scrollHeight 是在 Casper 上下文中,而不是在 casper.evaluate 中吗?
  • @ArtjomB。我添加了一个工作代码。事实上,我目前正在我正在做的一次抓取中使用它。它涉及调用 PhantomJS 中的底层代码。
  • 现在在gist.github.com/nwaomachux/35d1c424966fccd16ae1有一个使用 CasperJS 的 twitter 报废的工作副本@
【解决方案2】:

CasperJs 基于 PhantomJS,根据下面的讨论,无头浏览器不存在窗口对象。

您可以查看讨论here

【讨论】:

  • 至少,document 存在于页面上下文中。并且在第一次滚动工作。但是推文没有加载。
【解决方案3】:

您可以在 Twitter 上使用:

casper.scrollToBottom();
casper.wait(1000, function () {
    casper.capture("loadedContent.png");
});

但是如果你包含 jQuery... ,上面的代码就不行了!

var casper = require('casper').create({
    clientScripts: [
        'jquery-1.11.0.min.js'
    ]
});

脚本注入会阻止 Twitter 的无限滚动加载内容。在 BoingBoing.net 上,CasperJS scrollToBottom() 与 jQuery 一起工作而不会阻塞。这真的取决于网站。

但是,您可以在内容加载后注入 jQuery。

casper.scrollToBottom();
casper.wait(1000, function () {
    casper.capture("loadedContent.png");

    // Inject client-side jQuery library
    casper.options.clientScripts.push("jquery.js");

    // And use like so...
    var height = casper.evaluate(function () {
        return $(document).height();
    });
});

【讨论】:

    【解决方案4】:

    我从a previous answer采纳了这个

    var iterations = 5; //amount of pages to go through
    var timeToWait = 2000; //time to wait in milliseconds
    
    var last;
    var list = [];
    
    for (i = 0; i <= iterations; i++) {
        list.push(i);
    }
    
    //evaluate this in the browser context and pass the timer back to casperjs
    casper.thenEvaluate(function(iters, waitTime) {
        window.x = 0;
        var intervalID = setInterval(function() {
            console.log("Using setInternal " + window.x);
            window.scrollTo(0, document.body.scrollHeight); 
    
            if (++window.x === iters) {
                window.clearInterval(intervalID);
            }
        }, waitTime);
    }, iterations, timeToWait);
    
    casper.each(list, function(self, i) {
    
        self.wait(timeToWait, function() {
            last = i;
            this.echo('Using this.wait ' + i);
        });
    
    });
    
    casper.waitFor(function() {
        return (last === list[list.length - 1] && iterations === this.getGlobal('x'));
    }, function() {
        this.echo('All done.')
    });
    

    基本上发生的事情是我进入页面上下文,滚动到底部,然后等待 2 秒以加载内容。显然,我希望重复使用 casper.scrollToBottom() 或更复杂的应用程序,但加载时间不允许我实现这一点。

    【讨论】:

    • 无限滚动的概念在哪里? .你刚刚迭代了一个循环。
    猜你喜欢
    • 1970-01-01
    • 2012-06-01
    • 2012-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多