Как спарсить данные с одинаковым классом и id?
Всем добрый день. Подскажите, как спарсить данные (прикрепил скриншот) с этого сайта: https://www.flashscore.ru/match/llD7mNQ3/#odds-comparison;over-under;full-time
Есть код, который прокручивает страницу и берет её html, но не знаю как можно взять эти данные? Класс и id у всех одинаковый, подобраться к ним не могу. Подскажите, как можно это спарсить? Заранее спасибо.
Код:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
from bs4 import BeautifulSoup
def main(sec_wait):
driver = webdriver.Chrome()
wait = WebDriverWait(driver, sec_wait)
driver.get("https://www.flashscore.ru/match/llD7mNQ3/#odds-comparison;over-under;full-time")
driver.execute_script("window.scrollTo(0, 700);")
data = wait_to_find_id(driver, wait, "odds_ou_2.5", ".")
html2 = driver.page_source
soup2 = BeautifulSoup(html2, 'lxml')
# print(html2)
tochka_kef_first = soup2.find_all('table', id='odds_ou_2.5')
print(tochka_kef_first)
def wait_to_find_id(driver, wait, id_, text_to_find=""):
try:
element = wait.until(
EC.text_to_be_present_in_element((By.ID, id_), text_to_find)
)
return driver.find_element_by_id(id_).text
except TimeoutException:
driver.quit()
raise Exception(f"Element not found after {wait._timeout} sec of waiting")
except Exception as e:
driver.quit()
raise e
if __name__ == "__main__":
main(10)
Как спарсить эти данные? (P.S. Именно тотал 2.5)
на выходе просто иметь 4 числа: 1.78 | 1.79 | 2.15 | 2.14
Ответы (1 шт):
Автор решения: Александр
→ Ссылка
вот решение:
import re
import time
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
def wait_to_find_by(driver, wait, value=None, find="", by=By.ID, text=True):
"""Finds an element by By object and an expectation for
checking if the given text is present in the specified element.
If no match was found within the specified time, an Exception is raised.
inputs:
driver - instance of the selenium.webdriver.some_class.
wait - instance of the selenium.webdriver.support.ui.WebDriverWait.
value[None] - The By object of the element to be found.
find[""] - the text that must be present in the element.
by[By.ID] - selenium.webdriver.common.by.By object.
text[True] - if False, then returns the element itself, otherwise - element.text.
"""
try:
element = wait.until(
EC.text_to_be_present_in_element((by, value), find)
)
element = driver.find_element(by=by, value=value)
if text:
return element.text
return element
except TimeoutException:
driver.quit()
raise Exception(f"Element not found after {wait._timeout} sec of waiting")
except Exception as e:
driver.quit()
raise e
def main(sec_wait):
driver = webdriver.Chrome()
wait = WebDriverWait(driver, sec_wait)
driver.get("https://www.flashscore.ru/match/llD7mNQ3/"\
"#odds-comparison;over-under;full-time")
wait_to_find_by(driver, wait, "odds_ou_2.5")
soup = BeautifulSoup(driver.page_source, 'lxml')
total_25 = (soup.find("table", id="odds_ou_2.5")
.find_all("tr", class_=["odd", "even"]))
data = []
for bookmaker in total_25:
link = bookmaker.find("a", class_="elink").get("href")
if link.find("453") == -1:
continue
data.append([
bookmaker.find("span", class_=["up"]),
bookmaker.find("span", class_=["down"])
])
if len(data) != 1:
raise Exception("there is no needable bookmaker")
else:
element_up, element_down = data[0]
data_up = re.split(r"\[.+\]", element_up.get("eu"))
data_down = re.split(r"\[.+\]", element_down.get("eu"))
print("up:", " >> ".join(data_up))
print("down:", " >> ".join(data_down))
if __name__ == "__main__":
main(10)

