#!/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" # 人民法院制作民事诉讼文书样式(页面5-32) ADDITIONAL_PAGES = [ "https://www.court.gov.cn/susongyangshi/5.html", # 管辖 "https://www.court.gov.cn/susongyangshi/6.html", # 回避 "https://www.court.gov.cn/susongyangshi/7.html", # 诉讼参加人 "https://www.court.gov.cn/susongyangshi/8.html", # 证据 "https://www.court.gov.cn/susongyangshi/9.html", # 期间、送达 "https://www.court.gov.cn/susongyangshi/10.html", # 调解 "https://www.court.gov.cn/susongyangshi/11.html", # 保全和先予执行 "https://www.court.gov.cn/susongyangshi/12.html", # 对妨害民事诉讼的强制措施 "https://www.court.gov.cn/susongyangshi/13.html", # 诉讼费用 "https://www.court.gov.cn/susongyangshi/14.html", # 第一审普通程序 "https://www.court.gov.cn/susongyangshi/15.html", # 简易程序 "https://www.court.gov.cn/susongyangshi/16.html", # 简易程序中的小额诉讼 "https://www.court.gov.cn/susongyangshi/17.html", # 公益诉讼 "https://www.court.gov.cn/susongyangshi/18.html", # 第三人撤销之诉 "https://www.court.gov.cn/susongyangshi/19.html", # 执行异议之诉 "https://www.court.gov.cn/susongyangshi/20.html", # 第二审程序 "https://www.court.gov.cn/susongyangshi/22.html", # 选民资格案件 "https://www.court.gov.cn/susongyangshi/23.html", # 宣告失踪、宣告死亡案件 "https://www.court.gov.cn/susongyangshi/24.html", # 认定公民民事行为能力案件 "https://www.court.gov.cn/susongyangshi/25.html", # 认定财产无主案件 "https://www.court.gov.cn/susongyangshi/26.html", # 确认调解协议案件 "https://www.court.gov.cn/susongyangshi/27.html", # 实现担保物权案件 "https://www.court.gov.cn/susongyangshi/28.html", # 监护权特别程序案件 "https://www.court.gov.cn/susongyangshi/29.html", # 确认仲裁协议效力案件 "https://www.court.gov.cn/susongyangshi/30.html", # 撤销仲裁裁决案件 "https://www.court.gov.cn/susongyangshi/31.html", # 人身安全保护令案件 "https://www.court.gov.cn/susongyangshi/32.html", # 其他 "https://www.court.gov.cn/susongyangshi/34.html", # 督促程序 "https://www.court.gov.cn/susongyangshi/35.html", # 公示催告程序 "https://www.court.gov.cn/susongyangshi/36.html", # 执行程序 "https://www.court.gov.cn/susongyangshi/37.html", # 涉外民事诉讼程序的特别规定 "https://www.court.gov.cn/susongyangshi/38.html", # 期间 "https://www.court.gov.cn/susongyangshi/39.html", # 送达 "https://www.court.gov.cn/susongyangshi/40.html", # 民事裁定书 "https://www.court.gov.cn/susongyangshi/41.html", # 民事判决书 "https://www.court.gov.cn/susongyangshi/42.html", # 其他 "https://www.court.gov.cn/susongyangshi/43.html", # 当事人申请再审案件 "https://www.court.gov.cn/susongyangshi/44.html", # 被遗漏的必须共同进行诉讼的当事人申请再审案件 "https://www.court.gov.cn/susongyangshi/45.html", # 案外人申请再审案件 "https://www.court.gov.cn/susongyangshi/46.html", # 人民法院依职权再审案件 "https://www.court.gov.cn/susongyangshi/47.html", # 人民检察院抗诉再审案件 "https://www.court.gov.cn/susongyangshi/48.html", # 检察建议再审案件 "https://www.court.gov.cn/susongyangshi/49.html", # 小额诉讼再审案件 "https://www.court.gov.cn/susongyangshi/50.html", # 其他 "https://www.court.gov.cn/susongyangshi/51.html", # 申请执行及委托执行 "https://www.court.gov.cn/susongyangshi/52.html", # 限制出境措施 "https://www.court.gov.cn/susongyangshi/53.html", # 执行中止与终结 "https://www.court.gov.cn/susongyangshi/54.html", # 执行金钱给付 "https://www.court.gov.cn/susongyangshi/55.html", # 执行财产交付及完成行为 "https://www.court.gov.cn/susongyangshi/56.html", # 审查不予执行申请 "https://www.court.gov.cn/susongyangshi/57.html", # 执行管辖 "https://www.court.gov.cn/susongyangshi/58.html", # 变更或追加执行当事人 "https://www.court.gov.cn/susongyangshi/59.html", # 执行协调与执行监督 "https://www.court.gov.cn/susongyangshi/60.html", # 承认和执行外国法院生效判决、裁定 "https://www.court.gov.cn/susongyangshi/61.html", # 认可和执行港澳台法院民事判决 "https://www.court.gov.cn/susongyangshi/62.html", # 承认和执行外国仲裁裁决 "https://www.court.gov.cn/susongyangshi/63.html", # 认可和执行港澳台仲裁裁决 "https://www.court.gov.cn/susongyangshi/64.html", # 国际民商事司法协助 "https://www.court.gov.cn/susongyangshi/65.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 main(): print("=" * 60) print("补充抓取人民法院制作类别模板") print("=" * 60) all_templates = {} # 先加载已有的URL列表 existing_file = '/root/.openclaw/workspace/知识库/法律文书模板库/全部模板URL列表.json' if os.path.exists(existing_file): with open(existing_file, 'r', encoding='utf-8') as f: existing = json.load(f) all_templates.update(existing) print(f"已加载 {len(existing)} 个已有模板") for list_url in ADDITIONAL_PAGES: print(f"\n正在处理: {list_url}") html = get_html(list_url) if len(html) < 1000 or "找不到" in html: print(f" 页面无效或不存在") continue # 提取当前页的模板 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'href="(/susongyangshi/\d+_\d+\.html)"') page_matches = page_pattern.findall(html) for page_url in set(page_matches): base_match = re.search(r'/(\d+)_(\d+)\.html', page_url) if base_match: base_num = base_match.group(1) # 尝试获取更多分页 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 len(next_html) < 1000 or "找不到" in next_html: break templates = extract_template_urls(next_html) new_count = 0 for url, title in templates: if url not in all_templates: all_templates[url] = title new_count += 1 if new_count > 0: print(f" 分页 {i}: 发现 {new_count} 个新模板") time.sleep(0.3) time.sleep(0.5) print(f"\n总共找到 {len(all_templates)} 个模板") # 保存URL列表 with open(existing_file, 'w', encoding='utf-8') as f: json.dump(all_templates, f, ensure_ascii=False, indent=2) print("URL列表已更新保存") # 打印所有URL供后续抓取 print("\n所有模板URL:") for url, title in sorted(all_templates.items(), key=lambda x: int(re.search(r'\d+', x[0]).group())): print(f" {title}: {url}") if __name__ == "__main__": main()