這個問題是在 6 月份提出的(需要協助在 Python 中使用 Selenium 抓取網站),我從那時起就使用該代碼,但遇到了問題。我認為PP網站已經發生了變化。這是我更新的代碼,在單擊 Fantasy Score 部分時似乎遇到了錯誤。該專案的目標只是從NBA球員那里刮取幻想分數
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import chromedriver_binary
# Assign PrizePicks URL
driver = webdriver.Chrome()
PATH = "C:\Program Files (x86)\chromedriver.exe"
############## PRIZEPICKS ################################################
#
url = "https://app.prizepicks.com/"
driver.get(url)
#this is to get rid of the pop-up box that shows after you Selenium opens the prizepicks page
driver.find_element(By.CLASS_NAME,"close").click()
#Selecting NBA
driver.find_element(By.XPATH, "//div[@class='name'][normalize-space()= 'NBA']").click()
#Clicking the Fantasy Score tab
driver.find_element(By.XPATH, "//div[@class='stat stat-active'][normalize-space()='Fantasy Score']").click()
driver.find_element(By.XPATH, "//div[@class='segment-selector-button']").click()
projections = WebDriverWait(driver, 20).until(
EC.presence_of_all_elements_located((By.CSS_SELECTOR, ".projection")))
#creating a list of the players names and fantasy scores to be collected
nbaPlayers = []
for projection in projections:
names = projection.find_element_by_xpath('.//div[@]').text
points= projection.find_element_by_xpath('.//div[@]').get_attribute('innerHTML')
print(names, points)
players = {
'Name': names,
'FantasyPoints': points,
}
nbaPlayers.append(players)
#Put the list in a dataframe
df = pd.DataFrame(nbaPlayers)
print(df)
driver.quit()
uj5u.com熱心網友回復:
您可以像這樣使用“find_element_by_xpath”。
driver.find_element_by_xpath("//*[text()='Fantasy Score']").click()
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/412142.html
標籤:
