【问题标题】:Scroll to see all dynamically added elements in div滚动查看 div 中所有动态添加的元素
【发布时间】:2016-05-02 11:34:43
【问题描述】:

我正在尝试在搜索完成后抓取 google play 商店页面,即这个: https://play.google.com/store/search?q=stackoverflow&c=apps

如您所见,当您滚动到页面末尾时会加载新应用,有时还会出现“显示更多”按钮。我正在制作一个需要进行所有这些滚动的小书签,但我无法让它工作。这是我当前的代码:

var appContainerDiv = document.getElementsByClassName("card-list two-cards")[0];
var numberOfApps = appContainerDiv.childNodes.length;

var numberOfApps2 = numberOfApps;

do {
    numberOfApps = numberOfApps2;
    window.scrollTo(0, document.body.scrollHeight);

    if (document.getElementById('show-more-button') !== null) {
        document.getElementById('show-more-button').click();
    }

    numberOfApps2 = document.getElementsByClassName("card-list two-cards")[0].childNodes.length;
}
while (numberOfApps2 > numberOfApps);

appContainerDiv 是所有应用都嵌套在其中的 div。

【问题讨论】:

    标签: javascript scroll web-scraping infinite-scroll


    【解决方案1】:
    var delay = 2000;
    var height = document.body.scrollHeight;
    window.scrollTo(0, document.body.scrollHeight);
    var interval = setInterval(function() {
    
        window.scrollTo(0, document.body.scrollHeight);
    
        if (document.getElementById('show-more-button') !== null) {
            document.getElementById('show-more-button').click();
        }
        if (height == document.body.scrollHeight) {
            clearInterval(interval);
    
            }
    
        } else {
            height = document.body.scrollHeight;
        }
    
    }, delay);
    

    【讨论】:

      猜你喜欢
      • 2011-06-27
      • 2015-12-19
      • 1970-01-01
      • 2012-02-11
      • 2020-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-23
      相关资源
      最近更新 更多