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

发布于 2025-01-20 21:39:20 字数 1857 浏览 0 评论 0原文

我设法获取了我想要的数据,但是现在我只需要它给我的前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']

因为您可以看到他给了我几个回报,但我只需要从上到下才需要前17个,我不需要以下这些名称,也不需要这些信息。我该怎么做?

I managed to get the data I wanted with selenium, but now I only need the first 17 data that it gives me, I need to make a kind of filter with this data, because I'm going to use conditions on top of them to use in another code.

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()

    

My result is below

['']
['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']

As you can see he gave me several returns but I only need the first 17 from top to bottom in order, I don't need these names below nor this information that is in front of them. How can I do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

美胚控场 2025-01-27 21:39:20

要提取前17个数据,您可以使用 >您可以使用以下任何一个定位器策略

  • 使用 css_selector ::

    /p>

      driver.get(“ https://blaze.com/pt/games/double”)
    print([my_elem.text for driver.find_elements中的my_elem(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 for driver.find_elements in.xpath,“ // div [@id ='roulette-recent'] // div [@class ='entry']”] [:17])
     
  • 控制台输出:

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

To extract the first 17 datas you can use List Slicing and you can use either of the following Locator Strategies:

  • Using 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])
    
  • Using 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])
    
  • Console Output:

    ['13', '4', '10', '3', '2', '10', '5', '14', '5', '6', '8', '4', '5', '8', '4', '7', '1']
    
删除会话 2025-01-27 21:39:20

我认为driver.find_elements返回结果作为数组,因此您可以使用数组进行结果。

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

I think driver.find_elements returns result as a array, so you can use array for result.

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文