我需要幫助,從一個不斷變化的span類中提取文本。我已經嘗試了所有的方法,Xpath、類名......但我永遠無法提取文本。
正如你在照片中看到的,我想從左上方提取數字("BB")。而在下面你可以看到,它是一個span類。這個價格是不斷變化的。 我已經嘗試用
driver.find_element_by_xpath['/html/body/div[1]/div[1]/div/div[3]/table/tbody/tr[1]/td[2]/div/div[3]/div[2]/div/span[1]/span>] .text
另外
driver.find_element_by_xpath('//span[@class="pane-legend-item-value-wrap"] ').text
Xpath應該是沒有問題的......但我不能提取文本。
uj5u.com熱心網友回復:
Tradingview中的所有元素都在一個iframe中。DOM不斷變化。
下面是代碼。有時它與options一起作業,有時與options1一起作業。
from selenium import webdriver
import time
driver = webdriver.Chrome(executable_path="path to chromedriver.exe")
driver.maximum_window()
driver.implicitly_wait(10)
driver.get("https://www.binance.com/es/trade/ETH_USDT?layout=pro")
time.sleep(40) # 要手動關閉彈出視窗,選擇 "tradingview "和 "Boillinger bands"。
driver.switch_to.frame(driver.find_element_by_xpath("//iframe[contains(@name,'tradingview') ]")
options = driver.find_elements_by_xpath("//table[@class='chart-markup-table']//tr[1]/div[@class='panel-legend']/div[contains(@class, 'study')]")
print(" from options: {}".format(len(options))
for opt in options:
title = opt.find_element_by_xpath("./span[1]"/span>).get_attribute("innerText"/span>)
value = opt.find_elements_by_xpath("./div//span[ contains(@class,'item-value')]/span/*")
valuelist = []
for val in value。
valuelist.append(val.get_attribute("innerText"/span>)
print(f"{title}: {valuelist}")
options1 = driver.find_elements_by_xpath("//div[@data-name='legend']/div[2]/div[2]/div[@data-name='legend-source-item'] "/span>)
print(" from options1: {}".format(len(options1))
for opt in options1:
title = opt.find_element_by_xpath(".//div[contains(@class,'titleWrapper')]"/span>).get_attribute("innerText"/span>)
value = opt.find_elements_by_xpath(".//div[contains(@class,'valueValue') ]/*")
valuelist = []
for val in value。
valuelist.append(val.get_attribute("innerText"/span>)
print(f"{title}: {valuelist}")
from options: 4
MA(7, close, 0)。['3449.2257']
MA(25, close, 0)。['3487.6052']
MA(99, close, 0)。['2713.4087']
BB(20, 2):['3556.2910', '4026.2328', '3086.3492']
from options1: 0] from options1.
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/327091.html
標籤:
