Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Selenium Python向下滚动所有帖子_Python_Selenium - Fatal编程技术网

使用Selenium Python向下滚动所有帖子

使用Selenium Python向下滚动所有帖子,python,selenium,Python,Selenium,我将Selenium与Python一起使用。我正在尝试向下滚动推特页面。但它直到页面结束时才向下滚动。它在中间停止,推特显示一条消息:“回到顶部”。它甚至没有显示页面上最后一个月的所有帖子。这是我的页面: users = ['BBCWorld'] username = browser.find_element_by_class_name("js-username-field") username.send_keys("username") password = brow

我将Selenium与Python一起使用。我正在尝试向下滚动推特页面。但它直到页面结束时才向下滚动。它在中间停止,推特显示一条消息:<代码>“回到顶部”。它甚至没有显示页面上最后一个月的所有帖子。这是我的页面:

users = ['BBCWorld']

    username = browser.find_element_by_class_name("js-username-field")
    username.send_keys("username")
    password = browser.find_element_by_class_name("js-password-field")
    password.send_keys("password")

    signin_click = WebDriverWait(browser, 500000).until(
            EC.element_to_be_clickable((By.XPATH, '//*[@id="page-container"]/div/div[1]/form/div[2]/button'))
        )
    signin_click.click()

    for user in users:
        # User's profile
        browser.get('https://twitter.com/' + user)

        time.sleep(0.5)

        SCROLL_PAUSE_TIME = 0.5

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

        while True:
            # Scroll down to bottom
            browser.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 = browser.execute_script("return document.body.scrollHeight")



        # Quit browser
        browser.quit()
你忘了这一点:

while True:
    # Scroll down to bottom
    browser.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 = browser.execute_script("return document.body.scrollHeight")

    # break condition
    if new_height == last_height:
        break
    last_height = new_height
此外,你还有
SCROLL\u PAUSE\u TIME=0.5
这并不多,当要加载的帖子数量变大时,推特的速度会变慢。你必须增加停顿时间。我会尝试
SCROLL\u PAUSE\u TIME=2


PS:使用硬编码暂停不是很有效。相反,当twitter加载新内容时,您可以尝试定位微调器或其他任何东西,并等待微调器消失。这会更优雅。

新高度在2-3次滚动后与上次高度相等,打破了循环。您是否增加了暂停?