我试图从tradingviews图表中获取数据。这是我正在使用的代码,目前正在运行
from selenium import webdriver
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.common.exceptions import TimeoutException
import time
MAMAarray = []
FAMAarray = []
# example option: add 'incognito' command line arg to options
option = webdriver.ChromeOptions()
option.add_argument("--incognito")
# create new instance of chrome in incognito mode
browser = webdriver.Chrome(chrome_options=option)
# go to website of interest
browser.get("https://www.tradingview.com/chart/vKzVQllW/#")
# wait up to 10 seconds for page to load
timeout = 10
try:
WebDriverWait(browser, timeout).until(EC.visibility_of_element_located((By.XPATH, "/html/body/div[1]")))
except TimeoutException:
print("Timed out waiting for page to load")
browser.quit()
time.sleep(5)
# get MAMA and FAMA values for BTCUSDT on binance
MAMA_element = browser.find_element_by_xpath("/html/body/div[1]/div[1]/div/div[1]/div/table/tbody/tr[1]/td[2]/div/div[3]/div[3]/div/span[3]")
FAMA_element = browser.find_element_by_xpath("/html/body/div[1]/div[1]/div/div[1]/div/table/tbody/tr[1]/td[2]/div/div[3]/div[3]/div/span[4]")
MAMAarray.append(float(MAMA_element.text))
FAMAarray.append(float(FAMA_element.text))
print(MAMAarray)
print(FAMAarray)
while True:
MAMA_element = browser.find_element_by_xpath(
"/html/body/div[1]/div[1]/div/div[1]/div/table/tbody/tr[1]/td[2]/div/div[3]/div[3]/div/span[3]")
FAMA_element = browser.find_element_by_xpath(
"/html/body/div[1]/div[1]/div/div[1]/div/table/tbody/tr[1]/td[2]/div/div[3]/div[3]/div/span[4]")
if (float(MAMA_element.text)) != MAMAarray[-1]:
MAMAarray.append(float(MAMA_element.text))
if (float(FAMA_element.text)) != FAMAarray[-1]:
FAMAarray.append(float(FAMA_element.text))
print(MAMAarray)
print(FAMAarray)
以下是输出:所以您可以看到数字的附加,但只有当我手动进入图表并将光标移到新的蜡烛(条)上时
^{pr2}$我该如何实现这一点的自动化,以便它自动获得最新的值,而不是我必须将光标悬停在新的条上以获取新值
编辑:
这是我要找的值,就像我说的,我可以通过脚本将其输出,但我无法在新蜡烛生成时更新该值
如果我没听错的话,你需要在元素上加一个
hover
。要做到这一点,您可以尝试以下操作:也可以查看Documentation
编辑:
在您的情况下,我将使用这两个选择器来获取您需要的信息:
^{pr2}$它们是动态变化的,不需要
hover
。在输出如下:
由于您希望从您提供的元素中动态获取,因此有一个解决方案。我不会用它来解决所有问题,因为它不是一个“干净”的问题。但对于我们的具体情况,我认为这是最好的。在
我发现,如果您将鼠标悬停在图形中的某个}都将向左移动。另外,如果
candle
,那么每一分钟图形更新时,所有{hover on (x,y)
位置是candles
之一的(y)位置,则得到candle
的值。这正是你需要的。我的建议是hover
到您需要的candle
,并且元素中的值将被更新,因为这个candle
的值将被更新(每1分钟一次)。如果你想在最短的延迟时间内得到信息,你必须将鼠标悬停在右边candle
。在使用此选项将鼠标移到蜡烛上:
此外,您可能需要
zoom in
图形。您可以尝试建议here,或者:相关问题 更多 >
编程相关推荐