This commit is contained in:
gabrielkheisa 2022-10-29 20:46:20 +07:00
commit 5d4ce5a41c
2 changed files with 102 additions and 0 deletions

51
README.md Normal file
View File

@ -0,0 +1,51 @@
<h1>Get market data from Yahoo Finance using Selenium</h1>
<h2>Requirements:</h2>
<ul>
<li>Python 3.6 or Python 3.7</li>
<li>Pip3</li>
<li>Selenium Python</li>
<li>Chromium driver binary</li>
<li>Jupyter Notebook (optional)</li>
</ul>
<h2>Example:</h2>
<ul>
<li>
USD-IDR
https://api.gabrielkheisa.xyz/stocks/usdidr/
</li>
<li>
Dow Jones Industrial Average SPDR ETF
https://api.gabrielkheisa.xyz/stocks/sp500/
</li>
<li>
Nasaq Composite
https://api.gabrielkheisa.xyz/stocks/ixic/
</li>
<li>
BTC-USD
https://api.gabrielkheisa.xyz/stocks/btcusd/
<pre>
{
"price": "20,752.78",
"price_change": "+483.29",
"change": "(+2.38%)",
"last_update": "(+2.38%)",
"server_update": "29-10-2022 20:13:26 GMT+7"
}
</pre>
</li>
<li>
ETH-USD
https://api.gabrielkheisa.xyz/stocks/ethusd/
</li>
<li>
Indeks Harga Saham Gabungan
https://api.gabrielkheisa.xyz/stocks/jkse/
</li>
</ul>

51
script.py Normal file
View File

@ -0,0 +1,51 @@
import requests
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
from datetime import datetime
import base64
from selenium.webdriver.chrome.options import Options
key = "API_KEY"
while(1):
try:
now = datetime.now()
dt_string = now.strftime("%d-%m-%Y %H:%M:%S")
options = Options()
options.add_argument('--headless')
options.add_argument('--disable-gpu')
browser = webdriver.Chrome(executable_path="/usr/bin/chromedriver", chrome_options=options)
browser.delete_all_cookies()
browser.get("https://finance.yahoo.com/quote/IDR=X")
time.sleep(5)
harga = browser.find_element_by_xpath("//*[@id=\"quote-header-info\"]/div[3]/div[1]/div/fin-streamer[1]").get_attribute('textContent')
price_change = browser.find_element_by_xpath("//*[@id=\"quote-header-info\"]/div[3]/div[1]/div/fin-streamer[2]/span").get_attribute('textContent')
change = browser.find_element_by_xpath("//*[@id=\"quote-header-info\"]/div[3]/div[1]/div/fin-streamer[3]/span").get_attribute('textContent')
last_update = browser.find_element_by_xpath("//*[@id=\"quote-market-notice\"]/span").get_attribute('textContent')
payload = harga + "=" + price_change + "=" + change + "=" + last_update + "=" + dt_string + " GMT+7" + "=" + key
print(payload)
payload_encoded = base64.b64encode(bytes(payload, 'utf-8'))
print(payload_encoded.decode('utf-8'))
response = requests.get('https://api.gabrielkheisa.xyz/stocks/usdidr/index.php?q='+str(payload_encoded.decode('ascii')))
browser.quit()
print("Sleep for 1 hour")
time.sleep(60*60)
except:
print("Error gak jelas, skip")
browser.quit()