law_processed / scrape.py
elijah12e3rfr's picture
Upload folder using huggingface_hub
fd0db5c verified
Raw History Blame Contribute Delete
1.36 kB
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import requests
import zipfile
import io
import os
import sys
def download_and_extract_all_chinese_laws(output_dir="laws_json"):
"""
1. 向 MOJ 批次下載所有中文法規 JSON(ZIP 包 chlaw.json)
2. 解壓到 output_dir
"""
ZIP_URL = "https://law.moj.gov.tw/api/data/chlaw.json.zip"
print(f"→ 開始下載:{ZIP_URL}")
resp = requests.get(ZIP_URL, stream=True)
resp.raise_for_status()
# 確認回傳內容是 ZIP 檔
content_type = resp.headers.get("Content-Type", "")
if "zip" not in content_type.lower():
print(f"❌ 下載結果不是 ZIP(Content-Type={content_type})", file=sys.stderr)
sys.exit(1)
# 建立輸出資料夾
os.makedirs(output_dir, exist_ok=True)
# 在記憶體中解壓
with zipfile.ZipFile(io.BytesIO(resp.content)) as zf:
members = [m for m in zf.namelist() if m.lower().endswith(".json")]
if not members:
print("❌ ZIP 內沒有找到任何 .json 檔", file=sys.stderr)
sys.exit(1)
for member in members:
print(f"→ 解壓:{member}")
zf.extract(member, output_dir)
print(f"✅ 已將所有法規 JSON 解壓到 `./{output_dir}/`")
if __name__ == "__main__":
download_and_extract_all_chinese_laws()