How to scrape Yahoo Finance stock data with Python

This time, we are going to learn the hands-on ability to scrape Yahoo financial data. Set-up python environment. Yahoo Finance page. For example, Alphabet Inc. (GOOG). Scrape & parse the page. The page we will be scrape. In the “network” page, we can’t find the json data. You can get all the data from the page source in script. ...

2021-09-02 · 2 min read · 358 words · KbWen · EN

Python 爬取每日股價(1)

如何取得每日的股價資訊 進入證交所每日收盤行情,選擇全部(不含…),可以看到有許多選項可點。 找到每日收盤行情 110.08.27每日收盤行情 點擊F12進入開發者環境,再點選Network,觀察我們要的數據資訊 Python及時股價 點選XHR找到傳送數據的Requests import requests url = "https://www.twse.com.tw/exchangeReport/MI_INDEX?response=json&date=20210827&type=ALLBUT0999&_=1630244648174" res = requests.get(url) res.json() 得到Json,並在data9找到全部的股票數據 {'alignsStyle1': [['center', 'center', 'center', 'center', 'center', 'center'], ... ... 'data9': [['0050', '元大台灣50', '16,875,047', '9,673', '2,328,482,421', '136.70', '138.50', '136.45', '138.15', '<p style= color:red>+</p>', '1.15', '138.15', '4', '138.20', '103', '0.00'], ['0051', '元大中型100', '17,810', '63', '1,003,448', '56.20', '56.60', '56.20', '56.60', '<p style= color:red>+</p>', '0.35', '56.55', '1', '56.60', '9', '0.00'], ... ... 'subtitle9': '110年08月27日每日收盤行情(全部(不含權證、牛熊證))'} 幾個重要的數據 ['0050', #股票代號 '元大台灣50', '16,875,047', #成交股數 '9,673', '2,328,482,421', '136.70', #開盤價 '138.50', #最高價 '136.45', #最低價 '138.15', #收盤價 '<p style= color:red>+</p>', '1.15', '138.15', '4', '138.20', '103', '0.00'], 輕鬆完成,在爬取過程中還是非常簡單的, ...

2021-08-29 · 1 min read · 96 words · KbWen · ZH

python 爬取及時股價

如何取得即時的股價資訊? 進入證交所提供的基本市況報導網站,在右上方輸入股票代號(以 2330 台積電為例)。你可以看到當日的最高、最低、成交價量以及最佳五檔等資訊。 此時在網頁上點選右鍵開啟 Inspect (檢查),打開 DevTools 並切換到 Network 欄位進行觀察: 你會發現網頁會持續發送 Request 到某個網址,名稱開頭是 getStockInfo,這就是我們要的數據源。 import requests url = "https://mis.twse.com.tw/stock/api/getStockInfo.jsp?ex_ch=tse_2330.tw" res = requests.get(url) res.json() 得到一個排列整齊的json {'queryTime': {'stockInfoItem': 4329, 'sessionKey': 'tse_2330.tw_20200908|', 'sessionStr': 'UserSession', 'sysDate': '20200908', 'sessionFromTime': -1, 'stockInfo': 2084673, 'showChart': False, 'sessionLatestTime': -1, 'sysTime': '12:05:35'}, 'referer': '', 'rtmessage': 'OK', 'exKey': 'if_tse_2330.tw_zh-tw.null', 'msgArray': [{'n': '台積電', 'g': '281_174_260_166_385_', 'u': '468.5000', 'mt': '060262', 'o': '428.0000', 'ps': '593', 'tk0': '2330.tw_tse_20200908_B_9998775018', 'a': '430.5000_431.0000_431.5000_432.0000_432.5000_', 'tlong': '1599537930000', 't': '12:05:30', 'it': '12', 'ch': '2330.tw', 'b': '430.0000_429.5000_429.0000_428.5000_428.0000_', 'f': '143_239_162_400_391_', 'w': '383.5000', 'pz': '428.0000', 'l': '427.5000', 'c': '2330', 'v': '16843', 'd': '20200908', 'tv': '-', 'tk1': '2330.tw_tse_20200908_B_9998774678', 'ts': '0', 'nf': '台灣積體電路製造股份有限公司', 'y': '426.0000', 'p': '0', 'i': '24', 'ip': '0', 'z': '-', 's': '-', 'h': '433.0000', 'ex': 'tse'}], 'userDelay': 5000, 'rtcode': '0000', 'cachedAlive': 7891} 在爬取網址時,不要亂刪後面的query parameters,除非你確認過差別是甚麼。 ...

2020-09-08 · 2 min read · 327 words · KbWen · ZH