【Day5】取得資訊-Python #2:抓股票資料

環境準備好,就可以抓Yahoo Finance上面的資料囉!為什麼不用Google呢?因為Google已經沒有提供API了,所以轉而向Yahoo要。 
本篇會教如何取得網路資料,會以Yahoo Finance為範例。會參考前輩的網站,這裡面很多寶物,大家可以去挖喔!


第 12 屆 iT 邦幫忙鐵人賽-原文:https://ithelp.ithome.com.tw/articles/10236119

開始前

先複習一下,我們Day4安裝完後,應該會在Pipfile內顯示:

[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true

[dev-packages]

[packages]
requests = "*"
numpy = "*"
pandas = "*"

[requires]
python_version = "3.8"

其中packages內顯示的就是我們專案所需的套件,確認我們都有安裝好,就去Pipfile.lock查看這三個是否有記錄,有的話表示安裝好。沒有的話使用pipenv install,他會根據packages去檢查Pipfile.lock是否都有,如果沒有或是版本不對就安裝。

取得股票資料

接著我們從Yahoo Finance那邊取得資料,需要呼叫他們的API:
https://query1.finance.yahoo.com/v8/finance/chart/2330.TW?period1=0&period2=1549258857&interval=1d&events=history&=hP2rOschxO0

首先,要設定好我們所要送出的請求。其中要設定的參數是股票代碼和日期。
股票代碼是2317.TW2317是鴻海的代碼,而TW是臺灣證券交易所,從Day3可以明顯看得出來代碼方式與Google不同,這個組合僅適合用在Yahoo Finance。period1起始日,數值為1970年後的秒數,而period2是結束日,也是1970年後的秒數。而這次範例,則是從2020/9/1算到2020/9/13,也就是1598889600~1599926400秒,因此period1=1598889600period2=1599926400

# API位置
address = "https://query1.finance.yahoo.com/v8/finance/chart/2317.TW?period1=0&period2=1599926400&interval=1d&events=history&=hP2rOschxO0"

設定好我們要取資料的來源後,接著就是用requestsget去跟Yahoo取資料:

# 使用requests 來跟遠端 API server 索取資料
response = requests.get(address)

最後再印出來,就會得到鴻海從9/1到9/13的股價:

print(response.text)

Yahoo Finance會回傳這樣的json檔,我們的歷史資料就取得囉~

{
    "chart": {
        "result": [
            {
                "meta": {
                    "currency": "TWD",
                    "symbol": "2317.TW",
                    "exchangeName": "TAI",
                    "instrumentType": "EQUITY",
                    "firstTradeDate": 726195600,
                    "regularMarketTime": 1599802203,
                    "gmtoffset": 28800,
                    "timezone": "CST",
                    "exchangeTimezoneName": "Asia/Taipei",
                    "regularMarketPrice": 78.5,
                    "chartPreviousClose": 76.9,
                    "priceHint": 2,
                    "currentTradingPeriod": {
                        "pre": {
                            "timezone": "CST",
                            "start": 1599786000,
                            "end": 1599786000,
                            "gmtoffset": 28800
                        },
                        "regular": {
                            "timezone": "CST",
                            "start": 1599786000,
                            "end": 1599802200,
                            "gmtoffset": 28800
                        },
                        "post": {
                            "timezone": "CST",
                            "start": 1599802200,
                            "end": 1599802200,
                            "gmtoffset": 28800
                        }
                    },
                    "dataGranularity": "1d",
                    "range": "",
                    "validRanges": [
                        "1d",
                        "5d",
                        "1mo",
                        "3mo",
                        "6mo",
                        "1y",
                        "2y",
                        "5y",
                        "10y",
                        "ytd",
                        "max"
                    ]
                },
                "timestamp": [
                    1598922000,
                    1599008400,
                    1599094800,
                    1599181200,
                    1599440400,
                    1599526800,
                    1599613200,
                    1599699600,
                    1599786000
                ],
                "indicators": {
                    "quote": [
                        {
                            "volume": [
                                18151104,
                                24934189,
                                41374369,
                                35776710,
                                20795188,
                                22118917,
                                22505471,
                                29117204,
                                16102726
                            ],
                            "high": [
                                77.30000305175781,
                                77.80000305175781,
                                79.30000305175781,
                                77.30000305175781,
                                77.5,
                                78.0999984741211,
                                77.9000015258789,
                                78.69999694824219,
                                78.5
                            ],
                            "low": [
                                76.4000015258789,
                                77.0,
                                77.5999984741211,
                                76.5999984741211,
                                76.80000305175781,
                                77.30000305175781,
                                76.5999984741211,
                                78.0,
                                78.0
                            ],
                            "close": [
                                77.0,
                                77.80000305175781,
                                77.69999694824219,
                                76.9000015258789,
                                77.30000305175781,
                                77.5,
                                77.9000015258789,
                                78.5,
                                78.5
                            ],
                            "open": [
                                76.9000015258789,
                                77.19999694824219,
                                78.80000305175781,
                                77.0,
                                76.80000305175781,
                                77.69999694824219,
                                77.30000305175781,
                                78.0,
                                78.5
                            ]
                        }
                    ],
                    "adjclose": [
                        {
                            "adjclose": [
                                77.0,
                                77.80000305175781,
                                77.69999694824219,
                                76.9000015258789,
                                77.30000305175781,
                                77.5,
                                77.9000015258789,
                                78.5,
                                78.5
                            ]
                        }
                    ]
                }
            }
        ],
        "error": null
    }
}

其中timestamp內的秒數轉成日期為:

  • 1598922000:2020年09月01日09點00分00秒
  • 1599008400:2020年09月02日09點00分00秒
  • 1599094800:2020年09月03日09點00分00秒
  • 1599181200:2020年09月04日09點00分00秒
  • 1599440400:2020年09月07日09點00分00秒
  • 1599526800:2020年09月08日09點00分00秒
  • 1599613200:2020年09月09日09點00分00秒
  • 1599699600:2020年09月10日09點00分00秒
  • 1599786000:2020年09月11日09點00分00秒
    而收盤價close為:
  • 77.0,
  • 77.80000305175781,
  • 77.69999694824219,
  • 76.9000015258789,
  • 77.30000305175781,
  • 77.5,
  • 77.9000015258789,
  • 78.5,
  • 78.5
    (看起來很奇怪,怎麼會是小數點這麼多位數呢?這個部分我也沒有仔細探究,如果有發現什麼,我再補上)

去證交所抓資料可以看到這幾天收盤價是:

  • 77
  • 77.8
  • 77.7
  • 76.9
  • 77.3
  • 77.5
  • 77.9
  • 78.5
  • 78.5
    如果把Yahoo的資料四捨五入到小數點第一位,資料就是會相同,所以Yahoo Finance資料是可以用的,不過需要整理。
    這樣我們的鴻海九月交易資料,就可以輕易的從Yahoo Finance取得!

備註:如果無法實行時

當無法執行時,應該是我們的執行環境設定部隊,像是我們這時候就要選擇正確的虛擬環境。
要執行我們這個檔案,可以用兩個方式:

在terminal中執行
先執行pipenv shell,他就會變成

(12-ithome) bash-3.2$ 

我們可以檢查環境,Mac OS預設是Python2,如果是我們設定的環境,應該會是Python3.8,所以可以輸入(12-ithome) bash-3.2$ python --version,結果會是:

Python 3.8.5

這時可以放心地執行檔案:

(12-ithome) bash-3.2$ python Day5_getStockFromYahoo.py

結果也會是:

{"chart":{"result":[{"meta":{"currency":"TWD","symbol":"2317.TW","exchangeName":"TAI","instrumentType":"EQUITY","firstTradeDate":726195600,"regularMarketTime":1599802203,"gmtoffset":28800,"timezone":"CST","exchangeTimezoneName":"Asia/Taipei","regularMarketPrice":78.5,"chartPreviousClose":76.9,"priceHint":2,"currentTradingPeriod":{"pre":{"timezone":"CST","start":1599786000,"end":1599786000,"gmtoffset":28800},"regular":{"timezone":"CST","start":1599786000,"end":1599802200,"gmtoffset":28800},"post":{"timezone":"CST","start":1599802200,"end":1599802200,"gmtoffset":28800}},"dataGranularity":"1d","range":"","validRanges":["1d","5d","1mo","3mo","6mo","1y","2y","5y","10y","ytd","max"]},"timestamp":[1598922000,1599008400,1599094800,1599181200,1599440400,1599526800,1599613200,1599699600,1599786000],"indicators":{"quote":[{"open":[76.9000015258789,77.19999694824219,78.80000305175781,77.0,76.80000305175781,77.69999694824219,77.30000305175781,78.0,78.5],"close":[77.0,77.80000305175781,77.69999694824219,76.9000015258789,77.30000305175781,77.5,77.9000015258789,78.5,78.5],"volume":[18151104,24934189,41374369,35776710,20795188,22118917,22505471,29117204,16102726],"high":[77.30000305175781,77.80000305175781,79.30000305175781,77.30000305175781,77.5,78.0999984741211,77.9000015258789,78.69999694824219,78.5],"low":[76.4000015258789,77.0,77.5999984741211,76.5999984741211,76.80000305175781,77.30000305175781,76.5999984741211,78.0,78.0]}],"adjclose":[{"adjclose":[77.0,77.80000305175781,77.69999694824219,76.9000015258789,77.30000305175781,77.5,77.9000015258789,78.5,78.5]}]}}],"error":null}}

在Visual Stadio Code直接運作:
 

https://ithelp.ithome.com.tw/upload/images/20200913/20103826wl2kzvRfQJ.png

選擇好執行的環境(可在上圖一調整),然後選擇執行Python就可以囉!

不過每次都需要這樣按有點麻煩,而且不能使用中斷點,所以我們直接在設定檔做好,每次按F5就可以囉!不知道的話,可以參考官方文件


參考網站:


 

~Copyright by Eyelash500~

IT技術文章EY*研究院
iT邦幫忙eyelash*睫毛
Blog睫毛*Relax
Facebook睫毛*Relax