我正在嘗試對匹配分數進行自動化處理。我正在使用硒和 BeautifulSoup。該腳本只讓我獲得前 10 場比賽,而不是其他比賽。我該如何解決?
from bs4 import BeautifulSoup
import lxml
from webdriver_manager.chrome import ChromeDriverManager
import time
import os
team_both = []
team_one = []
team_two = []
driver = webdriver.Chrome(ChromeDriverManager().install())
time.sleep(1)
os.system('clear')
time.sleep(1)
driver.get('https://www.bet365.de/#/IP/B1')
time.sleep(1)
driver.find_element_by_class_name('iip-IntroductoryPopup_Cross').click()
time.sleep(1)
driver.get('https://www.bet365.de/#/IP/B1')
time.sleep(1)
soup = BeautifulSoup(driver.page_source, 'lxml')
time.sleep(1)
tab_matches = soup.find_all('div', {"class" : "ovm-Fixture_Container"})
print(len(tab_matches))
uj5u.com熱心網友回復:
問題是您不滾動網頁,因此不會加載所有元素。我添加了將頁面滾動到底部的功能,它對我有用。我還洗掉了 BeautifulSoup,因為當您已經在使用 Selenium 時使用它是沒有意義的。
from webdriver_manager.chrome import ChromeDriverManager
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
team_both = []
team_one = []
team_two = []
driver = webdriver.Chrome(ChromeDriverManager().install())
time.sleep(1)
#os.system('clear')
time.sleep(1)
driver.get('https://www.bet365.de/#/IP/B1')
time.sleep(1)
driver.find_element_by_class_name('iip-IntroductoryPopup_Cross').click()
time.sleep(1)
driver.get('https://www.bet365.de/#/IP/B1')
time.sleep(1)
# Scroll to bottom
element = driver.find_element_by_class_name("ovm-OverviewScroller-enabled")
actions = ActionChains(driver)
actions.move_to_element(element).click_and_hold().perform()
time.sleep(5)
tab_matches = driver.find_elements(by=By.CLASS_NAME, value="ovm-Fixture_Container")
print(len(tab_matches))
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/438436.html
下一篇:如何使網頁停止加載并從中提取文本
