I need to write a small app to get the real-time quote on Money18.on.cc for my school project. I'm aiming get all the data shown . I have tried Selenium w/ a list of tickers ["700", "3690", "1"], but the outcome looks strange, e.g. sometimes all 3 results are the same or the order of the corresponding price is wrong. Here is my code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
webdriverPath = getFileContentItem("all_file_path.txt", "chromeWebdriverDir")
s = Service(webdriverPath)
driver = webdriver.Chrome(service = s)
driver.implicitly_wait(10)
driver.set_page_load_timeout(20)
print("Successful open browser...")
tickerList = ["700", "3690", "1"]
money18Link = "https://money18.on.cc/eng/info/hk/liveinfo_quote_00700.html"
print(f"money18Link:\n{money18Link}")
driver.get(money18Link)
driver.find_element(By.XPATH, '//div[@class="industryClose"]/img').click() # remove the AD from the main window
for ticker in tickerList:
driver.get(money18Link)
inputElem = driver.find_element(By.CSS_SELECTOR, '[class="stockInput stock-number"]')
inputElem = myElem = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.CSS_SELECTOR, '[class="stockInput stock-number"]')))
inputElem.click()
inputElem.clear()
inputElem.send_keys(ticker)
inputElem.send_keys(Keys.ENTER)
print(f"ticker: {ticker} is running...")
driver.switch_to.default_content()
curPx = driver.find_element(By.XPATH, '//div[@class="section-body"]/div[4]/div[1]/div/div/div[1]/span[@class="value"]').text
print(f"curPx: {curPx}")
A few result scenarios below but look strange, the stock price scrapped is not corresponding to its ticker:
Scen 1:
ticker: 700 is running...
curPx: 374.400
ticker: 3690 is running...
curPx: 374.400
ticker: 1 is running...
curPx: 374.400
Scen 2:
ticker: 700 is running...
curPx: 374.400
ticker: 3690 is running...
curPx: 374.400
ticker: 1 is running...
curPx: 155.200