---
# System prepended metadata

title: 統編查詢

---

### 統編查詢
```py=
import requests
import json.decoder

apiUrl = "https://data.gcis.nat.gov.tw/od/data/api/9D17AE0D-09B5-4732-A8F4-81ADED04B679"

# 必填參數
formatValue = "json"
topValue = 1

# 統編列表
with open("accounting.txt", "r") as file:
    businessAccountingNos = [line.strip().replace('"', '').replace(',', '') for line in file]

for businessAccountingNo in businessAccountingNos:
    params = {
        '$format': formatValue,
        '$filter': f'Business_Accounting_NO eq {businessAccountingNo}',
        '$top': topValue
    }

    response = requests.get(apiUrl, params=params)

    try:
        data = response.json()

        if data and len(data) > 0:
            companyName = data[0].get("Company_Name", "查無資料")
            print(f"統一編號 {businessAccountingNo} 對應的公司名稱為：{companyName}")
        else:
            print(f"統一編號 {businessAccountingNo} 查無資料")

    except json.decoder.JSONDecodeError as e:
        if response.text.strip() == "":
            print(f"統一編號 {businessAccountingNo} 的 API 回應為空: {e}")
        else:
            print(f"統一編號 {businessAccountingNo} 的 API 回應不是有效的 JSON 格式: {e}")

    except StopIteration:
        print("StopIteration: 0")

    except Exception as e:
        print(f"統一編號 {businessAccountingNo} 查詢時發生未知錯誤: {e}")
```