--- name: cpc-price-cli description: Query Taiwan CPC (中油) historical fuel prices via CLI. Use when user wants to check current or historical gas/fuel prices in Taiwan. tools: Read, Glob, Grep, Bash --- # CPC 油價查詢工具 查詢台灣中油(CPC)歷史油價的命令列工具,資料來源為[中油歷史油價頁面](https://www.cpc.com.tw/historyprice.aspx?n=2890)。 ## 安裝 ### Step 1: 安裝依賴 ```bash pip install requests beautifulsoup4 ``` ### Step 2: 下載腳本 ```bash # 下載腳本 wget https://raw.githubusercontent.com/timmy/cpc-price-cli/main/cpc_price.py -O /usr/local/bin/cpc-price chmod +x /usr/local/bin/cpc-price # 或使用 curl curl -o /usr/local/bin/cpc-price https://raw.githubusercontent.com/timmy/cpc-price-cli/main/cpc_price.py chmod +x /usr/local/bin/cpc-price ``` ## 使用方式 ### 查詢油價 ```bash # 表格輸出(預設,顯示約 7 筆每週調價紀錄) cpc-price # 只顯示最新一筆 cpc-price --latest # 日期轉西元年 cpc-price --ad # CSV 輸出 cpc-price -f csv # JSON 輸出 cpc-price -f json # JSON + 西元年 cpc-price -f json --ad ``` ### 參數說明 | 參數 | 說明 | |------|------| | `-f`, `--format` | 輸出格式:`table`(預設)、`csv`、`json` | | `--ad` | 將民國年份轉換為西元年份(民國年 + 1911) | | `--latest` | 只顯示最新一筆資料 | ## 輸出範例 ### 表格輸出(預設) ``` ┌────────────┬────────┬────────┬────────┬────────┐ │ 日期 │ 92無鉛 │ 95無鉛 │ 98無鉛 │ 超級 │ ├────────────┼────────┼────────┼────────┼────────┤ │ 115/03/16 │ 28.8 │ 30.1 │ 32.1 │ 30.8 │ │ 115/03/09 │ 29.0 │ 30.3 │ 32.3 │ 31.0 │ │ 115/03/02 │ 29.4 │ 30.7 │ 32.7 │ 31.4 │ └────────────┴────────┴────────┴────────┴────────┘ ``` ### JSON 輸出 ```json [ {"日期": "115/03/16", "92無鉛": "28.8", "95無鉛": "30.1", "98無鉛": "32.1", "超級": "30.8"}, {"日期": "115/03/09", "92無鉛": "29.0", "95無鉛": "30.3", "98無鉛": "32.3", "超級": "31.0"} ] ``` ## 注意事項 - 中油網站的 SSL 憑證缺少 Subject Key Identifier,因此使用 `verify=False` 略過驗證。 - 日期格式為民國年,例如 `115/03/16`,加上 `--ad` 後顯示為 `2026/03/16`。 - 若頁面 HTML 結構變動導致解析失敗,請檢查原始碼中的 `fetch_prices()` 函式。 ## 更新 ```bash # 重新下載最新版本 wget https://raw.githubusercontent.com/timmy/cpc-price-cli/main/cpc_price.py -O /usr/local/bin/cpc-price chmod +x /usr/local/bin/cpc-price ```