如何在 Python 中停止 While True 的无限循环?

How can I stop an infinite loop While True in Python?

我想从使用 JavaScript 的网站解析足球赔率,所以它不会一次下载所有数据,我必须使用慢速滚动来加载页面的其余部分,然后解析它。 我正在使用我在这个网站上找到的一个函数来向下滚动页面,但是这个函数创建了一个无限循环,我不知道如何停止它并继续我的代码。 我希望当页面到达我感兴趣的网页部分时停止滚动,然后继续解析数据。

我已经尝试制作以 break 结尾的 if 语句,但没有成功。

from selenium import webdriver
from bs4 import BeautifulSoup
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.common.by import By
from selenium.common.exceptions import StaleElementReferenceException
import time
import pandas as pd

class wait_for_more_than_n_elements(object):
    def __init__(self, locator, count):
        self.locator = locator
        self.count = count

    def __call__(self, driver):
        try:
            count = len(ec._find_elements(driver, self.locator))
            return count >= self.count
        except StaleElementReferenceException:
            return False

#Apri la pagina
driver = webdriver.Firefox(executable_path='/Applications/Python 3.7/geckodriver')
driver.get('https://www.eurobet.it/it/scommesse/?splash=false#!/calcio/it-serie-a/')
time.sleep(5)

# Doppia chance
dc_button = driver.find_element_by_link_text('doppia chance')
dc_button.click()
time.sleep(5)

# Page source for changing page
source_dc = driver.page_source
soup_dc = BeautifulSoup(source_dc, 'lxml')

# Scrolling down the page
wait = WebDriverWait(driver, 60)
wait.until(ec.invisibility_of_element_located((By.CSS_SELECTOR, "div.box-row-event:nth-child(7)")))

while True:
    results = driver.find_elements_by_class_name("box-row-event")
    print("Results count: %d" % len(results))

    # scroll to the last element
    driver.execute_script("arguments[0].scrollIntoView();", results[-1])

    # wait for more results to load
    wait.until(wait_for_more_than_n_elements((By.CLASS_NAME, 'box-row-event'), len(results)))

我希望循环在到达变量结果中的最后一个元素时结束,但不幸的是它继续循环并始终打印相同长度的变量结果。

您可以尝试使用以下代码退出循环。

results = 0
# loop until the number of results are equals to previous results
until results == len(driver.find_elements_by_class_name("box-row-event")) :
    results = driver.find_elements_by_class_name("box-row-event")
    print("Results count: %d" % len(results))

    # scroll to the last element
    driver.execute_script("arguments[0].scrollIntoView();", results[-1])

    # wait for more results to load
    wait.until(wait_for_more_than_n_elements((By.CLASS_NAME, 'box-row-event'), len(results)))

经过多次尝试,我终于找到了适合我的解决方案:

last_count = len(results)
while True:
    results = driver.find_elements_by_class_name("box-row-event")
    print("Results count: %d" % len(results))

    # scroll to the last element
    driver.execute_script("arguments[0].scrollIntoView();", results[-1])
    time.sleep(1)

    # wait for more results to load
    wait.until(wait_for_more_than_n_elements((By.CLASS_NAME, 'box-row-event'), len(results)))
    wait.until(ec.visibility_of_any_elements_located((By.CLASS_NAME, 'box-row-event')))
    time.sleep(1)

    #new count
    new_count = len(driver.find_elements_by_class_name("box-row-event"))

    if new_count == last_count:
        break

    last_count = new_count

当页面转到最后一个结果并最终下载所有结果时,循环即将中断。