gpt4 book ai didi

python - 如何在 Python 上使用 Selenium 从网站上抓取全名?

转载 作者:行者123 更新时间:2023-12-01 00:16:01 24 4
gpt4 key购买 nike

我对 Python 上的编码和学习 Selenium Webdriver 还很陌生。到目前为止,我已经得到了很多帮助,并且非常接近我想要的输出。

到目前为止,我能够获取球员的姓名缩写、大小盘数据和盘口。例如,我当前的输出如下所示:

Player                              Over       Line       Under

A. Radulov +127 2.5 -167
G. Landeskog -130 2.5 +100
etc.

但是,我希望最终输出显示所有玩家的全名:

Player                               Over        Line       Under

Alexander Radulov +127 2.5 -167
Gabriel Landeskog -130 2.5 +100
etc.

这是我当前的代码

import pandas as pd
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
import time

driver=webdriver.Chrome("C:\webdrivers\chromedriver.exe")
driver.maximize_window()
driver.get("https://www.betonline.ag/sportsbook/player-props")
WebDriverWait(driver,20).until(EC.frame_to_be_available_and_switch_to_it((By.ID,"builder")))
time.sleep(2)
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//li[@class='one-third one-third-remove']//a[./b[contains(.,'Over / Under')]]"))).click()
time.sleep(2)




WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"div[ng-if='selected.league']"))).click()
time.sleep(2)
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//li[@ng-repeat='league in leagues']/a[.//span[text()='NHL']]"))).click()
time.sleep(2)
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"div[ng-if^='selected.game']"))).click()
time.sleep(2)
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//li/a[.//div[text()='All Available']]"))).click()

WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//span[contains(.,'Shots on goal')]"))).click()



player=[]
Over=[]
line=[]
Under=[]
Playersname=WebDriverWait(driver,10).until(EC.presence_of_all_elements_located((By.XPATH,"//div[@class='div-table__row__cell hard--bottom hard--right ng-scope']//a[@class='ng-binding']")))
for players in Playersname:
player.append(players.text)

OverAndUnder=WebDriverWait(driver,10).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR,"a>b.milli.caps.ng-binding")))
count=int(len(OverAndUnder)/2)
x=0
for i in range(count):
Over.append(OverAndUnder[x].text)
Under.append(OverAndUnder[x+1].text)
x=x+2

lines=WebDriverWait(driver,10).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR,"div[ng-class*='overUnder']>b")))
for l in lines:
line.append(l.text)

df = pd.DataFrame({"Player":player,"Over":Over,"Line":line, "Under":Under})
print(df)

为了找到全名,我点击了玩家的名字并检查了数据。我确实找到了正确的数据,但我不确定如何正确解析它。

enter image description here

我希望最终输出将包含所有相同的数据,但玩家的全名而不是缩写的名字除外。预先感谢您提供的任何帮助或见解。

最佳答案

所需的元素是 Angular元素,因此要提取文本 98.72,您必须为 visibility_of_element_ located() 引入 WebDriverWait,并且您可以使用以下解决方案之一:

  • 使用 CSS_SELECTORtext 属性:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "player-stats-content.ng-isolate-scope[data='pStats']>p span:nth-of-type(2)"))).text)
  • 使用XPATHget_attribute():

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//player-stats-content[@class='ng-isolate-scope' and @data='pStats']/p[@class='text--center beta cap']//following::span[2]"))).get_attribute("innerHTML"))
  • 注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC

关于python - 如何在 Python 上使用 Selenium 从网站上抓取全名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59330373/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com