我已經撰寫了代碼,請參見以下幾行:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
from selenium.webdriver.common.by import By
PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get("https://www.flashscore.com/match/jBvNMej6/#match-summary")
print(driver.title)
driver.maximize_window() # For maximizing window
driver.implicitly_wait(10) # gives an implicit wait for 20 seconds
driver.find_element_by_id('onetrust-reject-all-handler').click()
time.sleep(2)
driver.find_element(By.CLASS_NAME,'previewShowMore.showMore').click()
main = driver.find_element(By.CLASS_NAME,'previewLine'[b[text()="Hot stat:"]]/text)
print(main.text)
time.sleep(2)
driver.close()
但是,我收到以下錯誤。
main = driver.find_element(By.CLASS_NAME,'previewLine'[b[text()="Hot stat:"]]/text) ^ SyntaxError: 無效語法
我該怎么做才能避免這種情況?
謝謝!:)
uj5u.com熱心網友回復:
那么,在這一行
main = driver.find_element(By.CLASS_NAME,'previewLine'[b[text()="Hot stat:"]]/text)
你做了一個很好的組合:)
你的定位器是絕對無效的。
此外,如果您想列印沒有“熱條紋”的段落文本,則需要從整個div(段落)文本中洗掉該字串。
這應該做你想要實作的目標:
main = driver.find_element(By.XPATH,"//div[@class='previewLine' and ./b[text()='Hot streak']]").text
main = main.replace('Hot streak','')
print(main)
uj5u.com熱心網友回復:
我沒有找到任何文本“熱統計:”。您必須附上找到的 html 代碼。
我假設您要檢索特定 previewLine 的文本?
main = driver.find_element(By.XPATH ,'//div[@]/b[contains(text(),"Hot streak")]/..')
print(main.text)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/386239.html
上一篇:網頁抓取僅提供頁面上的前4個元素
