Spaces:
Sleeping
Sleeping
Download app.py from drew3952/judicial_scraper_2: direct link, hf CLI and curl.
- Browser
- Download file 1.47 kB
-
https://huggingface.co/spaces/drew3952/judicial_scraper_2/resolve/main/app.py
- Command line
-
hf download hf://spaces/drew3952/judicial_scraper_2/app.py
-
curl -L -o app.py https://huggingface.co/spaces/drew3952/judicial_scraper_2/resolve/main/app.py
1.47 kB
| import os, re, tempfile | |
| from datetime import datetime | |
| import gradio as gr | |
| from scraper import crawl # 下面的 scraper.py | |
| def run(keyword, start_date, end_date): | |
| tmpdir = tempfile.mkdtemp() | |
| ts = datetime.now().strftime("%Y%m%d_%H%M%S") | |
| safe_kw = re.sub(r"[^\w\u4e00-\u9fff]+", "_", keyword.strip())[:10] or "result" | |
| work_dir = os.path.join(tmpdir, f"judgments_{safe_kw}_{ts}") | |
| path = crawl(keyword, start_date, end_date, work_dir) | |
| log_path = os.path.join(work_dir, "debug.log") | |
| log_text = open(log_path, encoding="utf-8").read() if os.path.exists(log_path) else "(no log)" | |
| return path, log_text | |
| # ---------- Gradio ---------- | |
| with gr.Blocks(title="裁判書爬蟲") as demo: | |
| gr.Markdown("## 📄 司法院裁判書 PDF 批次下載(民國年輸入)") | |
| with gr.Row(): | |
| keyword = gr.Textbox(label="關鍵字", value="陳凱文") | |
| start_dt = gr.Textbox(label="起始日(民國年-月-日)", value="112-05-31") | |
| end_dt = gr.Textbox(label="結束日(民國年-月-日)", value="114-05-31") | |
| run_btn = gr.Button("開始爬蟲") | |
| file_out = gr.File(label="下載結果(ZIP 或 LOG)") | |
| log_out = gr.Textbox(label="執行紀錄", lines=15) | |
| run_btn.click(run, [keyword, start_dt, end_dt], [file_out, log_out]) | |
| if __name__ == "__main__": | |
| demo.queue().launch( | |
| server_name="0.0.0.0", | |
| server_port=int(os.environ.get("PORT", 7860)), | |
| show_error=True, | |
| ) | |