使用Python和Selenium进行网页爬取Tradingview图表

2024-09-29 02:26:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从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}$

我该如何实现这一点的自动化,以便它自动获得最新的值,而不是我必须将光标悬停在新的条上以获取新值

编辑:

这是我要找的值,就像我说的,我可以通过脚本将其输出,但我无法在新蜡烛生成时更新该值

this


Tags: totextfromimportdivbrowserbyselenium
1条回答
网友
1楼 · 发布于 2024-09-29 02:26:57

如果我没听错的话,你需要在元素上加一个hover。要做到这一点,您可以尝试以下操作:

hover = ActionChains(webDriver).move_to_element(element_to_hover_over)
hover.perform()

也可以查看Documentation

编辑:

在您的情况下,我将使用这两个选择器来获取您需要的信息:

^{pr2}$

它们是动态变化的,不需要hover。在

image

输出如下:

7420.06x2 // you have to split this string and extract 7420.06
7424.00×0.007977999746799469 // and here 7424.00

由于您希望从您提供的元素中动态获取,因此有一个解决方案。我不会用它来解决所有问题,因为它不是一个“干净”的问题。但对于我们的具体情况,我认为这是最好的。在

我发现,如果您将鼠标悬停在图形中的某个candle,那么每一分钟图形更新时,所有{}都将向左移动。另外,如果hover on (x,y)位置是candles之一的(y)位置,则得到candle的值。这正是你需要的。我的建议是hover到您需要的candle,并且元素中的值将被更新,因为这个candle的值将被更新(每1分钟一次)。如果你想在最短的延迟时间内得到信息,你必须将鼠标悬停在右边candle。在

Example

使用此选项将鼠标移到蜡烛上:

move_to_element_with_offset(to_element, xoffset, yoffset)

Move the mouse by an offset of the specified element. Offsets are relative to the top-left corner of the element. Args:

  • to_element: The WebElement to move to.
  • xoffset: X offset to move to.
  • yoffset: Y offset to move to.

此外,您可能需要zoom in图形。您可以尝试建议here,或者:

webdriver.execute_script("window.scrollBy(0, 150);") // 0 - xPos, 150 - yPos (values are in px).

相关问题 更多 >