如何使用 selenium python 缓慢向下滚动网页?

How to scroll down web page slowly using selenium python?

我想使用 selenium 向下滚动网页。找到这个:How can I scroll a web page using selenium webdriver in python?

采用此处显示的代码:

SCROLL_PAUSE_TIME = 0.5

# Get scroll height
last_height = driver.execute_script("return document.body.scrollHeight")

while True:
    # Scroll down to bottom
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

    # Wait to load page
    time.sleep(SCROLL_PAUSE_TIME)

    # Calculate new scroll height and compare with last scroll height
    new_height = driver.execute_script("return document.body.scrollHeight")
    if new_height == last_height:
        break
    last_height = new_height

它工作正常。但是由于上面的代码,我在我的主要代码中发现了一些问题。我想解析推特。如果 twitter 帐户很长,在网页的 html 代码中有一些 twits。并非此帐户的所有推文。

示例:我向下滚动网页,在 html 网页代码中仅包含那些对我可见(我可以看到)的推文。由于这件事,我无法理解所有的想法。上面的代码快速滚动页面。我怎样才能减慢滚动速度?

我试图解决它并写了愚蠢的代码:

    last_height = driver.execute_script("return document.body.scrollHeight")
    print(last_height)

    # Scroll down to bottom
    y = 600
    finished = False
    while True:
        for timer in range(0, 100):
            driver.execute_script("window.scrollTo(0, " + str(y) + ")")
            y += 600
            sleep(1)
            new_height = driver.execute_script("return document.body.scrollHeight")
            print(new_height, last_height)

            if new_height == last_height: #on the first iteration new_height equals last_height
                print('stop')
                finished = True
                break
            last_height = new_height
        if finished:
            break

此代码无效。在第一次迭代中 new_height 等于 last_height 请帮助我。
如果你能修复我的代码,请修复它。如果你能写出另一个优雅的解决方案,请写出来。

更新:

这个滚动必须是无限的。例如:我向下滚动 facebook 帐户,直到我完全滚动它。这就是为什么我有 last_height 和 new_height 变量。在我的代码中,当 last_height 等于 new_height 时,这意味着页面已经滚动到末尾,我们可以停止滚动它(我们可以退出)。但我错过了一些东西。我的代码不起作用。

我在 Twitter 机器人上工作过,当您向下滚动时,它会更新页面的 HTML 并从上面删除一些推文。我使用的算法是:

  • 为 URL 秒的推文创建一个空列表。
  • 收集可用的推文,然后为每条推文检查它的 URL 是否在列表中,如果不在列表中则添加它并对推文的内容进行你想要的处理,否则忽略该推文。
  • 获取页面高度current_height = DriverWrapper.cd.execute_script("return document.body.scrollHeight")
  • 向下滚动页面,如果new_height == current_height结束,否则从第二步开始重复..