#!/usr/bin/env python3 """ 法律文书模板抓取脚本 - 最高人民法院 抓取所有诉讼文书样式模板 """ import os import re import time import json import subprocess from urllib.parse import urljoin BASE_URL = "https://www.court.gov.cn" LISTING_PAGES = [ # 人民法院制作民事诉讼文书样式(当事人参考类) "https://www.court.gov.cn/susongyangshi/66.html", # 管辖 "https://www.court.gov.cn/susongyangshi/67.html", # 回避 "https://www.court.gov.cn/susongyangshi/68.html", # 诉讼参加人 "https://www.court.gov.cn/susongyangshi/69.html", # 证据 "https://www.court.gov.cn/susongyangshi/70.html", # 期间、送达 "https://www.court.gov.cn/susongyangshi/71.html", # 调解 "https://www.court.gov.cn/susongyangshi/72.html", # 保全和先予执行 "https://www.court.gov.cn/susongyangshi/73.html", # 对妨害民事诉讼的强制措施 "https://www.court.gov.cn/susongyangshi/74.html", # 诉讼费用 "https://www.court.gov.cn/susongyangshi/75.html", # 第一审普通程序 "https://www.court.gov.cn/susongyangshi/76.html", # 简易程序 "https://www.court.gov.cn/susongyangshi/77.html", # 简易程序中的小额诉讼 "https://www.court.gov.cn/susongyangshi/78.html", # 公益诉讼 "https://www.court.gov.cn/susongyangshi/79.html", # 第三人撤销之诉 "https://www.court.gov.cn/susongyangshi/80.html", # 执行异议之诉 "https://www.court.gov.cn/susongyangshi/81.html", # 第二审程序 "https://www.court.gov.cn/susongyangshi/83.html", # 选民资格案件 "https://www.court.gov.cn/susongyangshi/84.html", # 宣告失踪、宣告死亡案件 "https://www.court.gov.cn/susongyangshi/85.html", # 认定公民民事行为能力案件 "https://www.court.gov.cn/susongyangshi/86.html", # 认定财产无主案件 "https://www.court.gov.cn/susongyangshi/87.html", # 确认调解协议案件 "https://www.court.gov.cn/susongyangshi/88.html", # 实现担保物权案件 "https://www.court.gov.cn/susongyangshi/89.html", # 监护权特别程序案件 "https://www.court.gov.cn/susongyangshi/90.html", # 确认仲裁协议效力案件 "https://www.court.gov.cn/susongyangshi/91.html", # 撤销仲裁裁决案件 "https://www.court.gov.cn/susongyangshi/92.html", # 人身安全保护令案件 "https://www.court.gov.cn/susongyangshi/93.html", # 其他非诉程序 "https://www.court.gov.cn/susongyangshi/94.html", # 审判监督程序 "https://www.court.gov.cn/susongyangshi/95.html", # 督促程序 "https://www.court.gov.cn/susongyangshi/96.html", # 公示催告程序 "https://www.court.gov.cn/susongyangshi/97.html", # 执行程序 "https://www.court.gov.cn/susongyangshi/98.html", # 涉外民事诉讼程序的特别规定 # 行政文书样式 "https://www.court.gov.cn/susongyangshi/102.html", # 国家赔偿文书样式 "https://www.court.gov.cn/susongyangshi/100.html", # 本院赔偿文书样式 "https://www.court.gov.cn/susongyangshi/101.html", # 赔偿委员会文书样式 ] def get_html(url): """使用curl获取页面HTML""" try: result = subprocess.run( ['curl', '-s', '-L', '-A', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36', url], capture_output=True, text=True, timeout=30 ) return result.stdout except Exception as e: print(f"Error fetching {url}: {e}") return "" def extract_template_urls(html): """从列表页面提取模板详情页URL""" pattern = r'href="(/susongyangshi/xiangqing/\d+\.html)"[^>]*>([^<]+)' matches = re.findall(pattern, html) urls = [] for url, title in matches: urls.append((url, title.strip())) return urls def get_all_pages_for_listing(base_list_url): """获取列表的所有分页""" pages = [base_list_url] html = get_html(base_list_url) # 查找分页链接模式 like 8_2.html, 8_3.html page_pattern = re.compile(r'href="(/susongyangshi/\d+_\d+\.html)"') page_urls = set(page_pattern.findall(html)) # 提取基础页码(如 8_2 中的 8) base_num_pattern = re.compile(r'/(\d+)_\d+\.html') for url in page_urls: match = base_num_pattern.search(url) if match: page_num = int(match.group(1)) # 添加所有分页 for i in range(2, 100): # 假设最多99页 next_page = f"/susongyangshi/{page_num}_{i}.html" next_html = get_html(BASE_URL + next_page) if "找不到" in next_html or not next_html: break pages.append(BASE_URL + next_page) return pages def main(): print("=" * 60) print("法律文书模板抓取开始") print("=" * 60) all_templates = {} for list_url in LISTING_PAGES: print(f"\n正在处理: {list_url}") html = get_html(list_url) # 提取当前页的模板 templates = extract_template_urls(html) print(f" 发现 {len(templates)} 个模板") for url, title in templates: if url not in all_templates: all_templates[url] = title # 处理分页 page_pattern = re.compile(r'/(\d+_\d+)\.html"') page_nums = set(page_pattern.findall(html)) for page_str in page_nums: base_num = page_str.split('_')[0] for i in range(2, 100): next_url = f"{list_url.rsplit('/', 1)[0]}/{base_num}_{i}.html" next_html = get_html(next_url) if "找不到" in next_html or len(next_html) < 1000: break templates = extract_template_urls(next_html) for url, title in templates: if url not in all_templates: all_templates[url] = title time.sleep(0.5) print(f"\n总共找到 {len(all_templates)} 个模板") # 保存URL列表 with open('/root/.openclaw/workspace/知识库/法律文书模板库/全部模板URL列表.json', 'w', encoding='utf-8') as f: json.dump(all_templates, f, ensure_ascii=False, indent=2) print("URL列表已保存") return all_templates if __name__ == "__main__": main()