如何过滤设置查询限制的 Selenium 结果?

How to filter Selenium results setting a query limit?

我设法用 selenium 得到了我想要的数据,但现在我只需要它给我的前 17 个数据,我需要用这些数据做一种过滤器,因为我要使用条件在它们之上以在另一个代码中使用。

from ctypes.wintypes import PINT
from logging import root
from tkinter import N
from hyperlink import URL
from numpy import number
from selenium import webdriver
import selenium
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.expected_conditions import presence_of_element_located
import time

url = "https://blaze.com/pt/games/double"

#absolute path
firefox_driver_path = "/Users/Antônio/Desktop/roletarobo/geckodriver.exe"
firefox_options = Options()
firefox_options.add_argument("--headless")

webdriver = webdriver.Firefox(
    executable_path = firefox_driver_path,
    options = firefox_options
)

with webdriver as driver:
    # timeout
    wait = WebDriverWait(driver, 1)

    # retrieve data
    driver.get(url)

    #wait
    wait.until(presence_of_element_located((By.ID, "roulette-recent")))

    results = driver.find_elements(by=By.CLASS_NAME, value='entry')#find_elements_by_css_selector('#roulette .sm-box .number')
    for quote in results:
      quoteArr = quote.text.split('\n')
      print(quoteArr)
    

    driver.close()

    

我的结果如下

['']
['13']
['4']
['11']
['11']
['13']
['6']
['5']
['9']
['14']
['5']
['']
['12']
['10']
['5']
['']
['3']
['13']
['']
['']
['Douglas', 'R$ 39.48']
['MSK', 'R$ 25.27']
['Marcz10', 'R$ 23.69']
['Jeferson ☘️', 'R$ 19.74']
['Pai_daBlaze', 'R$ 12.35']
['Lucianotelles1993', 'R$ 11.84']
['Alexandra souza', 'R$ 11.84']
['Taynara Luna', 'R$ 9.87']
['Mylla coutinho', 'R$ 9.87']
['Marcos smith', 'R$ 9.87']

如你所见,他给了我几个returns,但我只需要从上到下的前17个,我不需要下面的这些名字,也不需要他们前面的这些信息。我该怎么做?

我认为driver.find_elements returns结果是一个数组,所以你可以使用数组作为结果。

new_results = results[0:17]

a[start:stop]  # items start through stop-1
a[start:]      # items start through the rest of the array
a[:stop]       # items from the beginning through stop-1
a[:]           # a copy of the whole array

要提取前 17 个数据,您可以使用 List Slicing and you can use either of the following :

  • 使用CSS_SELECTOR:

    driver.get("https://blaze.com/pt/games/double")
    print([my_elem.text for my_elem in driver.find_elements(By.CSS_SELECTOR, "div#roulette-recent div.entry")][:17])
    
  • 使用 XPATH:

    driver.get("https://blaze.com/pt/games/double")
    print([my_elem.text for my_elem in driver.find_elements(By.XPATH, "//div[@id='roulette-recent']//div[@class='entry']")][:17])
    
  • 控制台输出:

    ['13', '4', '10', '3', '2', '10', '5', '14', '5', '6', '8', '4', '5', '8', '4', '7', '1']