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.
JavaScript
x
47
47
1
from ctypes.wintypes import PINT
2
from logging import root
3
from tkinter import N
4
from hyperlink import URL
5
from numpy import number
6
from selenium import webdriver
7
import selenium
8
from selenium.webdriver.firefox.options import Options
9
from selenium.webdriver.common.by import By
10
from selenium.webdriver.common.keys import Keys
11
from selenium.webdriver.support.ui import WebDriverWait
12
from selenium.webdriver.support.expected_conditions import presence_of_element_located
13
import time
14
15
url = "https://blaze.com/pt/games/double"
16
17
#absolute path
18
firefox_driver_path = "/Users/Antônio/Desktop/roletarobo/geckodriver.exe"
19
firefox_options = Options()
20
firefox_options.add_argument("--headless")
21
22
webdriver = webdriver.Firefox(
23
executable_path = firefox_driver_path,
24
options = firefox_options
25
)
26
27
with webdriver as driver:
28
# timeout
29
wait = WebDriverWait(driver, 1)
30
31
# retrieve data
32
driver.get(url)
33
34
#wait
35
wait.until(presence_of_element_located((By.ID, "roulette-recent")))
36
37
results = driver.find_elements(by=By.CLASS_NAME, value='entry')#find_elements_by_css_selector('#roulette .sm-box .number')
38
for quote in results:
39
quoteArr = quote.text.split('n')
40
print(quoteArr)
41
42
43
driver.close()
44
45
46
47
My result is below
JavaScript
1
31
31
1
['']
2
['13']
3
['4']
4
['11']
5
['11']
6
['13']
7
['6']
8
['5']
9
['9']
10
['14']
11
['5']
12
['']
13
['12']
14
['10']
15
['5']
16
['']
17
['3']
18
['13']
19
['']
20
['']
21
['Douglas', 'R$ 39.48']
22
['MSK', 'R$ 25.27']
23
['Marcz10', 'R$ 23.69']
24
['Jeferson ☘️', 'R$ 19.74']
25
['Pai_daBlaze', 'R$ 12.35']
26
['Lucianotelles1993', 'R$ 11.84']
27
['Alexandra souza', 'R$ 11.84']
28
['Taynara Luna', 'R$ 9.87']
29
['Mylla coutinho', 'R$ 9.87']
30
['Marcos smith', 'R$ 9.87']
31
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?
Advertisement
Answer
To extract the first 17 datas you can use List Slicing and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
JavaScript131driver.get("https://blaze.com/pt/games/double")
2print([my_elem.text for my_elem in driver.find_elements(By.CSS_SELECTOR, "div#roulette-recent div.entry")][:17])
3
Using XPATH:
JavaScript131driver.get("https://blaze.com/pt/games/double")
2print([my_elem.text for my_elem in driver.find_elements(By.XPATH, "//div[@id='roulette-recent']//div[@class='entry']")][:17])
3
Console Output:
JavaScript121['13', '4', '10', '3', '2', '10', '5', '14', '5', '6', '8', '4', '5', '8', '4', '7', '1']
2