diff --git a/DataEngineering/Chapter12/12.8/dataflow/crawler/taiwan_stock_price.py b/DataEngineering/Chapter12/12.8/dataflow/crawler/taiwan_stock_price.py index f2246ab..71c09e7 100644 --- a/DataEngineering/Chapter12/12.8/dataflow/crawler/taiwan_stock_price.py +++ b/DataEngineering/Chapter12/12.8/dataflow/crawler/taiwan_stock_price.py @@ -169,7 +169,7 @@ def crawler_twse( """ logger.info("crawler_twse") # headers 中的 Request url - url = "https://www.twse.com.tw/exchangeReport/MI_INDEX?response=json&date={date}&type=ALL" + url = "https://www.twse.com.tw/rwd/zh/afterTrading/MI_INDEX?response=json&date={date}&type=ALL" url = url.format( date=date.replace("-", "") ) @@ -179,33 +179,13 @@ def crawler_twse( res = requests.get( url, headers=twse_header() ) - # 2009 年以後的資料, 股價在 response 中的 data9 - # 2009 年以後的資料, 股價在 response 中的 data8 - # 不同格式, 在證交所的資料中, 是很常見的, - # 沒資料的情境也要考慮進去,例如現在週六沒有交易,但在 2007 年週六是有交易的 df = pd.DataFrame() + # 證交所資料格式改變,更新程式碼 try: - if "data9" in res.json(): - df = pd.DataFrame( - res.json()["data9"] - ) - colname = res.json()[ - "fields9" - ] - elif "data8" in res.json(): - df = pd.DataFrame( - res.json()["data8"] - ) - colname = res.json()[ - "fields8" - ] - elif res.json()["stat"] in [ - "查詢日期小於93年2月11日,請重新查詢!", - "很抱歉,沒有符合條件的資料!", - ]: - pass - except Exception as e: - logger.error(e) + data = res.json()["tables"][8] + df = pd.DataFrame(data["data"]) + colname = data["fields"] + except BaseException: return pd.DataFrame() if len(df) == 0: diff --git a/DataEngineering/Chapter14/14.3/14.3.4/airflow/dataflow/crawler/taiwan_stock_price.py b/DataEngineering/Chapter14/14.3/14.3.4/airflow/dataflow/crawler/taiwan_stock_price.py index f2246ab..71c09e7 100644 --- a/DataEngineering/Chapter14/14.3/14.3.4/airflow/dataflow/crawler/taiwan_stock_price.py +++ b/DataEngineering/Chapter14/14.3/14.3.4/airflow/dataflow/crawler/taiwan_stock_price.py @@ -169,7 +169,7 @@ def crawler_twse( """ logger.info("crawler_twse") # headers 中的 Request url - url = "https://www.twse.com.tw/exchangeReport/MI_INDEX?response=json&date={date}&type=ALL" + url = "https://www.twse.com.tw/rwd/zh/afterTrading/MI_INDEX?response=json&date={date}&type=ALL" url = url.format( date=date.replace("-", "") ) @@ -179,33 +179,13 @@ def crawler_twse( res = requests.get( url, headers=twse_header() ) - # 2009 年以後的資料, 股價在 response 中的 data9 - # 2009 年以後的資料, 股價在 response 中的 data8 - # 不同格式, 在證交所的資料中, 是很常見的, - # 沒資料的情境也要考慮進去,例如現在週六沒有交易,但在 2007 年週六是有交易的 df = pd.DataFrame() + # 證交所資料格式改變,更新程式碼 try: - if "data9" in res.json(): - df = pd.DataFrame( - res.json()["data9"] - ) - colname = res.json()[ - "fields9" - ] - elif "data8" in res.json(): - df = pd.DataFrame( - res.json()["data8"] - ) - colname = res.json()[ - "fields8" - ] - elif res.json()["stat"] in [ - "查詢日期小於93年2月11日,請重新查詢!", - "很抱歉,沒有符合條件的資料!", - ]: - pass - except Exception as e: - logger.error(e) + data = res.json()["tables"][8] + df = pd.DataFrame(data["data"]) + colname = data["fields"] + except BaseException: return pd.DataFrame() if len(df) == 0: diff --git a/DataEngineering/Chapter5/5.2/src/twse_crawler.py b/DataEngineering/Chapter5/5.2/src/twse_crawler.py index 2e41218..d18896f 100644 --- a/DataEngineering/Chapter5/5.2/src/twse_crawler.py +++ b/DataEngineering/Chapter5/5.2/src/twse_crawler.py @@ -108,7 +108,7 @@ def crawler_twse( """ # headers 中的 Request url url = ( - "https://www.twse.com.tw/exchangeReport/MI_INDEX" + "https://www.twse.com.tw/rwd/zh/afterTrading/MI_INDEX" "?response=json&date={date}&type=ALL" ) url = url.format( @@ -126,30 +126,12 @@ def crawler_twse( ): # 如果 date 是周末,會回傳很抱歉,沒有符合條件的資料! return pd.DataFrame() - # 2009 年以後的資料, 股價在 response 中的 data9 - # 2009 年以後的資料, 股價在 response 中的 data8 - # 不同格式, 在證交所的資料中, 是很常見的, - # 沒資料的情境也要考慮進去,例如現在週六沒有交易,但在 2007 年週六是有交易的 + df = pd.DataFrame() + # 證交所資料格式改變,更新程式碼 try: - if "data9" in res.json(): - df = pd.DataFrame( - res.json()["data9"] - ) - colname = res.json()[ - "fields9" - ] - elif "data8" in res.json(): - df = pd.DataFrame( - res.json()["data8"] - ) - colname = res.json()[ - "fields8" - ] - elif res.json()["stat"] in [ - "查詢日期小於93年2月11日,請重新查詢!", - "很抱歉,沒有符合條件的資料!", - ]: - return pd.DataFrame() + data = res.json()["tables"][8] + df = pd.DataFrame(data["data"]) + colname = data["fields"] except BaseException: return pd.DataFrame() diff --git a/DataEngineering/Chapter5/5.4/financialdata/twse_crawler.py b/DataEngineering/Chapter5/5.4/financialdata/twse_crawler.py index 74641a9..77936d4 100644 --- a/DataEngineering/Chapter5/5.4/financialdata/twse_crawler.py +++ b/DataEngineering/Chapter5/5.4/financialdata/twse_crawler.py @@ -111,7 +111,7 @@ def crawler_twse( """ # headers 中的 Request url url = ( - "https://www.twse.com.tw/exchangeReport/MI_INDEX" + "https://www.twse.com.tw/rwd/zh/afterTrading/MI_INDEX" "?response=json&date={date}&type=ALL" ) url = url.format( @@ -129,30 +129,12 @@ def crawler_twse( ): # 如果 date 是周末,會回傳很抱歉,沒有符合條件的資料! return pd.DataFrame() - # 2009 年以後的資料, 股價在 response 中的 data9 - # 2009 年以後的資料, 股價在 response 中的 data8 - # 不同格式, 在證交所的資料中, 是很常見的, - # 沒資料的情境也要考慮進去,例如現在週六沒有交易,但在 2007 年週六是有交易的 + df = pd.DataFrame() + # 證交所資料格式改變,更新程式碼 try: - if "data9" in res.json(): - df = pd.DataFrame( - res.json()["data9"] - ) - colname = res.json()[ - "fields9" - ] - elif "data8" in res.json(): - df = pd.DataFrame( - res.json()["data8"] - ) - colname = res.json()[ - "fields8" - ] - elif res.json()["stat"] in [ - "查詢日期小於93年2月11日,請重新查詢!", - "很抱歉,沒有符合條件的資料!", - ]: - return pd.DataFrame() + data = res.json()["tables"][8] + df = pd.DataFrame(data["data"]) + colname = data["fields"] except BaseException: return pd.DataFrame() diff --git a/DataEngineering/Chapter5/5.5/5.5.5/financialdata/crawler/taiwan_stock_price.py b/DataEngineering/Chapter5/5.5/5.5.5/financialdata/crawler/taiwan_stock_price.py index 767e6eb..f8165e7 100644 --- a/DataEngineering/Chapter5/5.5/5.5.5/financialdata/crawler/taiwan_stock_price.py +++ b/DataEngineering/Chapter5/5.5/5.5.5/financialdata/crawler/taiwan_stock_price.py @@ -200,7 +200,7 @@ def crawler_twse( """ logger.info("crawler_twse") # headers 中的 Request url - url = "https://www.twse.com.tw/exchangeReport/MI_INDEX?response=json&date={date}&type=ALL" + url = "https://www.twse.com.tw/rwd/zh/afterTrading/MI_INDEX?response=json&date={date}&type=ALL" url = url.format( date=date.replace("-", "") ) @@ -210,33 +210,13 @@ def crawler_twse( res = requests.get( url, headers=twse_header() ) - # 2009 年以後的資料, 股價在 response 中的 data9 - # 2009 年以後的資料, 股價在 response 中的 data8 - # 不同格式, 在證交所的資料中, 是很常見的, - # 沒資料的情境也要考慮進去,例如現在週六沒有交易,但在 2007 年週六是有交易的 df = pd.DataFrame() + # 證交所資料格式改變,更新程式碼 try: - if "data9" in res.json(): - df = pd.DataFrame( - res.json()["data9"] - ) - colname = res.json()[ - "fields9" - ] - elif "data8" in res.json(): - df = pd.DataFrame( - res.json()["data8"] - ) - colname = res.json()[ - "fields8" - ] - elif res.json()["stat"] in [ - "查詢日期小於93年2月11日,請重新查詢!", - "很抱歉,沒有符合條件的資料!", - ]: - pass - except Exception as e: - logger.error(e) + data = res.json()["tables"][8] + df = pd.DataFrame(data["data"]) + colname = data["fields"] + except BaseException: return pd.DataFrame() if len(df) == 0: diff --git a/DataEngineering/Chapter5/5.6/5.6.3/financialdata/crawler/taiwan_stock_price.py b/DataEngineering/Chapter5/5.6/5.6.3/financialdata/crawler/taiwan_stock_price.py index 9913401..87d1562 100644 --- a/DataEngineering/Chapter5/5.6/5.6.3/financialdata/crawler/taiwan_stock_price.py +++ b/DataEngineering/Chapter5/5.6/5.6.3/financialdata/crawler/taiwan_stock_price.py @@ -206,7 +206,7 @@ def crawler_twse( """ logger.info("crawler_twse") # headers 中的 Request url - url = "https://www.twse.com.tw/exchangeReport/MI_INDEX?response=json&date={date}&type=ALL" + url = "https://www.twse.com.tw/rwd/zh/afterTrading/MI_INDEX?response=json&date={date}&type=ALL" url = url.format( date=date.replace("-", "") ) @@ -216,33 +216,13 @@ def crawler_twse( res = requests.get( url, headers=twse_header() ) - # 2009 年以後的資料, 股價在 response 中的 data9 - # 2009 年以後的資料, 股價在 response 中的 data8 - # 不同格式, 在證交所的資料中, 是很常見的, - # 沒資料的情境也要考慮進去,例如現在週六沒有交易,但在 2007 年週六是有交易的 df = pd.DataFrame() + # 證交所資料格式改變,更新程式碼 try: - if "data9" in res.json(): - df = pd.DataFrame( - res.json()["data9"] - ) - colname = res.json()[ - "fields9" - ] - elif "data8" in res.json(): - df = pd.DataFrame( - res.json()["data8"] - ) - colname = res.json()[ - "fields8" - ] - elif res.json()["stat"] in [ - "查詢日期小於93年2月11日,請重新查詢!", - "很抱歉,沒有符合條件的資料!", - ]: - pass - except Exception as e: - logger.error(e) + data = res.json()["tables"][8] + df = pd.DataFrame(data["data"]) + colname = data["fields"] + except BaseException: return pd.DataFrame() if len(df) == 0: diff --git a/DataEngineering/Chapter7/7.2/7.2.1/financialdata/crawler/taiwan_stock_price.py b/DataEngineering/Chapter7/7.2/7.2.1/financialdata/crawler/taiwan_stock_price.py index 9913401..87d1562 100644 --- a/DataEngineering/Chapter7/7.2/7.2.1/financialdata/crawler/taiwan_stock_price.py +++ b/DataEngineering/Chapter7/7.2/7.2.1/financialdata/crawler/taiwan_stock_price.py @@ -206,7 +206,7 @@ def crawler_twse( """ logger.info("crawler_twse") # headers 中的 Request url - url = "https://www.twse.com.tw/exchangeReport/MI_INDEX?response=json&date={date}&type=ALL" + url = "https://www.twse.com.tw/rwd/zh/afterTrading/MI_INDEX?response=json&date={date}&type=ALL" url = url.format( date=date.replace("-", "") ) @@ -216,33 +216,13 @@ def crawler_twse( res = requests.get( url, headers=twse_header() ) - # 2009 年以後的資料, 股價在 response 中的 data9 - # 2009 年以後的資料, 股價在 response 中的 data8 - # 不同格式, 在證交所的資料中, 是很常見的, - # 沒資料的情境也要考慮進去,例如現在週六沒有交易,但在 2007 年週六是有交易的 df = pd.DataFrame() + # 證交所資料格式改變,更新程式碼 try: - if "data9" in res.json(): - df = pd.DataFrame( - res.json()["data9"] - ) - colname = res.json()[ - "fields9" - ] - elif "data8" in res.json(): - df = pd.DataFrame( - res.json()["data8"] - ) - colname = res.json()[ - "fields8" - ] - elif res.json()["stat"] in [ - "查詢日期小於93年2月11日,請重新查詢!", - "很抱歉,沒有符合條件的資料!", - ]: - pass - except Exception as e: - logger.error(e) + data = res.json()["tables"][8] + df = pd.DataFrame(data["data"]) + colname = data["fields"] + except BaseException: return pd.DataFrame() if len(df) == 0: diff --git a/DataEngineering/Chapter7/7.6/financialdata/financialdata/crawler/taiwan_stock_price.py b/DataEngineering/Chapter7/7.6/financialdata/financialdata/crawler/taiwan_stock_price.py index 0a916fa..c456713 100644 --- a/DataEngineering/Chapter7/7.6/financialdata/financialdata/crawler/taiwan_stock_price.py +++ b/DataEngineering/Chapter7/7.6/financialdata/financialdata/crawler/taiwan_stock_price.py @@ -206,7 +206,7 @@ def crawler_twse( """ logger.info("crawler_twse") # headers 中的 Request url - url = "https://www.twse.com.tw/exchangeReport/MI_INDEX?response=json&date={date}&type=ALL" + url = "https://www.twse.com.tw/rwd/zh/afterTrading/MI_INDEX?response=json&date={date}&type=ALL" url = url.format( date=date.replace("-", "") ) @@ -216,33 +216,13 @@ def crawler_twse( res = requests.get( url, headers=twse_header() ) - # 2009 年以後的資料, 股價在 response 中的 data9 - # 2009 年以後的資料, 股價在 response 中的 data8 - # 不同格式, 在證交所的資料中, 是很常見的, - # 沒資料的情境也要考慮進去,例如現在週六沒有交易,但在 2007 年週六是有交易的 df = pd.DataFrame() + # 證交所資料格式改變,更新程式碼 try: - if "data9" in res.json(): - df = pd.DataFrame( - res.json()["data9"] - ) - colname = res.json()[ - "fields9" - ] - elif "data8" in res.json(): - df = pd.DataFrame( - res.json()["data8"] - ) - colname = res.json()[ - "fields8" - ] - elif res.json()["stat"] in [ - "查詢日期小於93年2月11日,請重新查詢!", - "很抱歉,沒有符合條件的資料!", - ]: - pass - except Exception as e: - logger.error(e) + data = res.json()["tables"][8] + df = pd.DataFrame(data["data"]) + colname = data["fields"] + except BaseException: return pd.DataFrame() if len(df) == 0: diff --git a/DataEngineering/Chapter8/8.1.4/financialdata/crawler/taiwan_stock_price.py b/DataEngineering/Chapter8/8.1.4/financialdata/crawler/taiwan_stock_price.py index 9913401..87d1562 100644 --- a/DataEngineering/Chapter8/8.1.4/financialdata/crawler/taiwan_stock_price.py +++ b/DataEngineering/Chapter8/8.1.4/financialdata/crawler/taiwan_stock_price.py @@ -206,7 +206,7 @@ def crawler_twse( """ logger.info("crawler_twse") # headers 中的 Request url - url = "https://www.twse.com.tw/exchangeReport/MI_INDEX?response=json&date={date}&type=ALL" + url = "https://www.twse.com.tw/rwd/zh/afterTrading/MI_INDEX?response=json&date={date}&type=ALL" url = url.format( date=date.replace("-", "") ) @@ -216,33 +216,13 @@ def crawler_twse( res = requests.get( url, headers=twse_header() ) - # 2009 年以後的資料, 股價在 response 中的 data9 - # 2009 年以後的資料, 股價在 response 中的 data8 - # 不同格式, 在證交所的資料中, 是很常見的, - # 沒資料的情境也要考慮進去,例如現在週六沒有交易,但在 2007 年週六是有交易的 df = pd.DataFrame() + # 證交所資料格式改變,更新程式碼 try: - if "data9" in res.json(): - df = pd.DataFrame( - res.json()["data9"] - ) - colname = res.json()[ - "fields9" - ] - elif "data8" in res.json(): - df = pd.DataFrame( - res.json()["data8"] - ) - colname = res.json()[ - "fields8" - ] - elif res.json()["stat"] in [ - "查詢日期小於93年2月11日,請重新查詢!", - "很抱歉,沒有符合條件的資料!", - ]: - pass - except Exception as e: - logger.error(e) + data = res.json()["tables"][8] + df = pd.DataFrame(data["data"]) + colname = data["fields"] + except BaseException: return pd.DataFrame() if len(df) == 0: