使用 selenium 和 python 抓取的实时网站
Real time websites scraping with selenium and python
我在实时网站上进行交易。现在我想不断地废弃它以提取实时股票或货币数据以使用 selenium 和 python 进行计算。我如何处理相同的问题。网页数据不断更新,并以突出显示的颜色显示在附加图像中。每次数据更改时,我都应该能够提取它并在代码中进行一些计算。请帮助我实现同样的目标。
捕获 1D 段的 Nifty50 的动态变化值。
您应该使用以下 XPath:
//button[text()='1D']/ancestor::nav//following-sibling::section/descendant::a[text()='Nifty 50']/../following-sibling::td/span
您的有效代码为:
driver.maximize_window()
wait = WebDriverWait(driver, 20)
driver.get('https://in.investing.com/')
i = 0
while True:
time.sleep(5)
oneD_Nifty50 = wait.until(EC.visibility_of_element_located((By.XPATH, "//button[text()='1D']/ancestor::nav//following-sibling::section/descendant::a[text()='Nifty 50']/../following-sibling::td/span")))
print(oneD_Nifty50.text)
i = i + 1
if i == 10:
break
else:
continue
进口:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
输出:
17,828.80
17,828.65
17,828.40
17,828.70
17,828.70
17,827.20
17,827.70
17,827.70
17,828.80
17,828.80
Process finished with exit code 0
我在实时网站上进行交易。现在我想不断地废弃它以提取实时股票或货币数据以使用 selenium 和 python 进行计算。我如何处理相同的问题。网页数据不断更新,并以突出显示的颜色显示在附加图像中。每次数据更改时,我都应该能够提取它并在代码中进行一些计算。请帮助我实现同样的目标。
捕获 1D 段的 Nifty50 的动态变化值。
您应该使用以下 XPath:
//button[text()='1D']/ancestor::nav//following-sibling::section/descendant::a[text()='Nifty 50']/../following-sibling::td/span
您的有效代码为:
driver.maximize_window()
wait = WebDriverWait(driver, 20)
driver.get('https://in.investing.com/')
i = 0
while True:
time.sleep(5)
oneD_Nifty50 = wait.until(EC.visibility_of_element_located((By.XPATH, "//button[text()='1D']/ancestor::nav//following-sibling::section/descendant::a[text()='Nifty 50']/../following-sibling::td/span")))
print(oneD_Nifty50.text)
i = i + 1
if i == 10:
break
else:
continue
进口:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
输出:
17,828.80
17,828.65
17,828.40
17,828.70
17,828.70
17,827.20
17,827.70
17,827.70
17,828.80
17,828.80
Process finished with exit code 0