使用Selenium和Python从足球网站上取得一些数据
我正在尝试制作一个Python程序,该程序使用Selenium提取一些数据,首先我必须关闭两个警报,然后单击“显示所有匹配”按钮,最后我需要单击每个“统计数据”按钮(有多个,它们所有这些都具有相同的类名),可以从此表中提取特定行。
我需要为每个游戏提取以蓝色突出显示的4个值
“ https://i.sstatic.net/earyb.png” alt =“我 在我必须单击每个“统计数据”按钮的地方,从每个表中提取4个值,然后关闭窗口并移至下一个游戏。
这是我的代码
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import time
s=Service("C:/Users/dhias/OneDrive/Bureau/stgg/chromedriver.exe")
driver=webdriver.Chrome(service=s)
driver.get("https://www.soccerstats.com/matches.asp?matchday=1#")
driver.maximize_window()
time.sleep(1)
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[mode='primary']"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID,"steady-floating-button"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Show all matches']"))).click()
,我尝试此单击具有相同类名但无法使用
for element in driver.find_elements(By.XPATH,"//a[@class='myButton' and text()='stats']"):
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//a[@class='myButton' and text()='stats']"))).click()
网站的每个“统计”按钮: Soccerstats网站
I'm trying to make a Python program that extracts some data using Selenium where first I have to close two alerts then click on "Show all matches" button and finally I need to click on each "stats" button (there are multiple and they all have the same class name) to extract a specific row from this table.
I need to extract the 4 values highlighted in blue for each game
I already did the first two steps but now I'm stuck in the final one where I have to click on each "stats" button, extract the 4 values from each table then close the window and move to the next game.
Here is my code
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import time
s=Service("C:/Users/dhias/OneDrive/Bureau/stgg/chromedriver.exe")
driver=webdriver.Chrome(service=s)
driver.get("https://www.soccerstats.com/matches.asp?matchday=1#")
driver.maximize_window()
time.sleep(1)
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[mode='primary']"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID,"steady-floating-button"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Show all matches']"))).click()
and I tried this to click on every "stats" button that have the same class name but it didn't work
for element in driver.find_elements(By.XPATH,"//a[@class='myButton' and text()='stats']"):
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//a[@class='myButton' and text()='stats']"))).click()
link to the website : soccerstats website
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将链接保存到数组,然后单击之后,因为单击后您不在页面上具有链接的页面
save links to array and click after because after click you are not on the page that have the links anymore
您确定您需要在这里使用硒吗?您可以轻松地用熊猫和请求拉这些桌子。
输出:
You sure you need to be using Selenium here? You can easily pull those tables with pandas and requests.
Output: