# 4.5 台灣證券交易所API

這個API長得大概像這樣:

```
http://www.twse.com.tw/exchangeReport/STOCK_DAY?response=json&date=20160501&stockNo=2330
```

比較重要的地方是date這個參數, 基本上你給的值一定要是yyyyMMdd的形式, 但是真正作用的只有yyyy與MM, 因為他會把這段request解讀成你想要看**stockNo股票在yyyy年MM月的紀錄**, 所以dd基本上沒有太大意義, 但卻是不可少的部分.

```python
import requests
import time


TWSE_URL = 'http://www.twse.com.tw/exchangeReport/STOCK_DAY?response=json'


def get_web_content(stock_id, current_date):
    resp = requests.get(TWSE_URL + '&date=' + current_date + '&stockNo=' + stock_id)
    if resp.status_code != 200:
        return None
    else:
        return resp.json()


def get_data(stock_id, current_date):
    info = list()
    resp = get_web_content(stock_id, current_date)
    if resp is None:
        return None
    else:
        if resp['data']:
            for data in resp['data']:
                record = {
                    '日期': data[0],
                    '開盤價': data[3],
                    '收盤價': data[6],
                    '成交筆數': data[8]
                }
                info.append(record)
        return info


def main():
    stock_id = '2330'
    current_date = time.strftime('%Y%m%d')
    current_year = time.strftime('%Y')
    current_month = time.strftime('%m')
    print('Processing data for %s %s...' % (current_year, current_month))
    get_data(stock_id, current_date)
    collected_info = get_data(stock_id, current_date)
    for info in collected_info:
        print(info)


if __name__ == '__main__':
    main()
```

執行結果:

```javascript
Processing data for 2017 05...
{'日期': '106/05/02', '開盤價': '198.50', '收盤價': '196.50', '成交筆數': '15,718'}
{'日期': '106/05/03', '開盤價': '198.00', '收盤價': '198.00', '成交筆數': '9,259'}
{'日期': '106/05/04', '開盤價': '198.50', '收盤價': '198.00', '成交筆數': '8,787'}
{'日期': '106/05/05', '開盤價': '197.00', '收盤價': '197.50', '成交筆數': '6,078'}
{'日期': '106/05/08', '開盤價': '199.00', '收盤價': '202.50', '成交筆數': '15,190'}
{'日期': '106/05/09', '開盤價': '205.50', '收盤價': '203.50', '成交筆數': '15,467'}
{'日期': '106/05/10', '開盤價': '204.00', '收盤價': '205.50', '成交筆數': '11,094'}
{'日期': '106/05/11', '開盤價': '204.50', '收盤價': '207.50', '成交筆數': '16,478'}
{'日期': '106/05/12', '開盤價': '205.00', '收盤價': '206.00', '成交筆數': '7,515'}
{'日期': '106/05/15', '開盤價': '204.00', '收盤價': '206.00', '成交筆數': '8,141'}
{'日期': '106/05/16', '開盤價': '205.00', '收盤價': '204.50', '成交筆數': '11,066'}
{'日期': '106/05/17', '開盤價': '203.00', '收盤價': '204.00', '成交筆數': '9,323'}
{'日期': '106/05/18', '開盤價': '202.50', '收盤價': '203.50', '成交筆數': '6,983'}
{'日期': '106/05/19', '開盤價': '203.50', '收盤價': '203.00', '成交筆數': '6,014'}
{'日期': '106/05/22', '開盤價': '203.50', '收盤價': '205.00', '成交筆數': '5,278'}
{'日期': '106/05/23', '開盤價': '205.00', '收盤價': '205.00', '成交筆數': '7,003'}
{'日期': '106/05/24', '開盤價': '205.00', '收盤價': '205.50', '成交筆數': '5,141'}
{'日期': '106/05/25', '開盤價': '206.00', '收盤價': '207.00', '成交筆數': '7,406'}
{'日期': '106/05/26', '開盤價': '205.00', '收盤價': '207.00', '成交筆數': '8,059'}

Process finished with exit code 0
```

原始碼[點我](https://github.com/yotsuba1022/web-crawler-practice/blob/master/ch4/tw_stock_exchange.py)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://clu.gitbook.io/python-web-crawler-note/45-tai-wan-zheng-quan-jiao-yi-suo.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
