我想創建一個使用 selenium 從網站獲取資訊的腳本。
但是,如果它沒有找到資訊并顯示錯誤訊息,它會跳過該請求并繼續下一個請求。
from selenium import webdriver
import pandas as pd
import undetected_chromedriver as uc
list1 = [6019306,6049500,6051161,6022230,5776662,6151430]
for x in range(0, list1.count()):
while True:
try:
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
driver = uc.Chrome(options=options)
url = 'https://www.axie.tech/axie-pricing/' str(list1[x])
driver.get(url)
driver.implicitly_wait(10)
test = driver.find_element_by_xpath('//*[@id="root"]/div[1]/div[2]/div[2]/div/div/div[1]/div/div[1]/div[4]/div/div[3]/div/span').text
test = float(test[1:])
print(test)
driver.close()
except NoSuchElementException:
'This Value doesnt exist'
driver.close()
uj5u.com熱心網友回復:
有點不清楚你到底想通過這條線做什么test = float(test[1:])。
但是,要從網站串列中提取所需的文本,您需要誘導WebDriverWait for visibility_of_element_located()并且您可以使用以下定位器策略:
代碼塊:
options = webdriver.ChromeOptions() options.add_argument("start-maximized") driver = uc.Chrome(options=options) list1 = [6019306, 6049500, 6051161, 6022230, 5776662, 6151430] for x in range(0, len(list1)): try: url = 'https://www.axie.tech/axie-pricing/' str(list1[x]) driver.get(url) print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//span[text()='Reasonable']//following::div[1]//span[contains(@class, 'MuiTypography-root')]"))).text) except TimeoutException: continue driver.quit()控制臺輸出:
Ξ0.01 Ξ0.012 Ξ0.0162 Ξ0.026注意:您必須添加以下匯入:
from selenium.webdriver.chrome.options import Options from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC from selenium.common.exceptions import TimeoutException
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/438793.html
標籤:Python python-3.x 硒 硒网络驱动程序
上一篇:Tkinter只會顯示我通過filedialog.askopenfilename選擇的第一張照片
下一篇:使用Python事件顯示影像
