溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

python中的selenium如何實(shí)現(xiàn)自動(dòng)向下滾動(dòng)頁(yè)面并指定最大滑動(dòng)距離

發(fā)布時(shí)間:2022-02-14 09:50:29 來(lái)源:億速云 閱讀:1047 作者:小新 欄目:開(kāi)發(fā)技術(shù)

這篇文章給大家分享的是有關(guān)python中的selenium如何實(shí)現(xiàn)自動(dòng)向下滾動(dòng)頁(yè)面并指定最大滑動(dòng)距離的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。

需要selenium控制的chrome向下滑動(dòng),自動(dòng)加載一些內(nèi)容,核心代碼是:

browser.execute_script("window.scrollBy(0,300)")

這行可以向下滑動(dòng)300個(gè)像素

需要的工具函數(shù)如下:

def roll_window_to_bottom(browser, stop_length=None, step_length=500):
    """selenium 滾動(dòng)當(dāng)前頁(yè)面,向下滑
    :param browser: selenium的webdriver
    :param stop_length: 滑動(dòng)的最大值
    :param step_length: 每次滑動(dòng)的值
    """
    original_top = 0
    while True:  # 循環(huán)向下滑動(dòng)
        if stop_length:
            if stop_length - step_length < 0:
                browser.execute_script("window.scrollBy(0,{})".format(stop_length))
                break
            stop_length -= step_length
        browser.execute_script("window.scrollBy(0,{})".format(step_length))
        time.sleep(0.5 + random.random())  # 停頓一下
        check_height = browser.execute_script(
            "return document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop;")
        if check_height == original_top:  # 判斷滑動(dòng)后距頂部的距離與滑動(dòng)前距頂部的距離
            break
        original_top = check_height

使用示例:

from selenium import webdriver
import time
import random


def roll_window_to_bottom(browser, stop_length=None, step_length=500):
    """selenium 滾動(dòng)當(dāng)前頁(yè)面,向下滑
    :param browser: selenium的webdriver
    :param stop_length: 滑動(dòng)的最大值
    :param step_length: 每次滑動(dòng)的值
    """
    original_top = 0
    while True:  # 循環(huán)向下滑動(dòng)
        if stop_length:
            if stop_length - step_length < 0:
                browser.execute_script("window.scrollBy(0,{})".format(stop_length))
                break
            stop_length -= step_length

        browser.execute_script("window.scrollBy(0,{})".format(step_length))
        time.sleep(0.5 + random.random())  # 停頓一下
        check_height = browser.execute_script(
            "return document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop;")
        if check_height == original_top:  # 判斷滑動(dòng)后距頂部的距離與滑動(dòng)前距頂部的距離
            break
        original_top = check_height


def main():
    option = webdriver.ChromeOptions()
    option.add_argument('lang=zh_CN.UTF-8')  # 設(shè)置
    browser = webdriver.Chrome(chrome_options=option, desired_capabilities={"page_load_strategy": "none"})
    browser.get("http://news.baidu.com/")
    roll_window_to_bottom(browser, stop_length=700)


if __name__ == '__main__':
    main()

感謝各位的閱讀!關(guān)于“python中的selenium如何實(shí)現(xiàn)自動(dòng)向下滾動(dòng)頁(yè)面并指定最大滑動(dòng)距離”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI