#!/usr/bin/env python3 """ 律师业务操作指引 爬取与PDF生成脚本 来源: 全国律协(acla.org.cn) 和 上海律协(lawyers.org.cn) """ import os import re import time import json import subprocess from pathlib import Path # 目标目录 BASE_DIR = Path("/root/.openclaw/workspace/知识库/律师业务操作指引") SHANGHAI_DIR = BASE_DIR / "上海律协" ACLA_DIR = BASE_DIR / "全国律协" MARKDOWN_DIR = BASE_DIR / "markdown" PDF_DIR = BASE_DIR SHANGHAI_DIR.mkdir(parents=True, exist_ok=True) ACLA_DIR.mkdir(parents=True, exist_ok=True) MARKDOWN_DIR.mkdir(parents=True, exist_ok=True) def cmd(c): result = subprocess.run(c, shell=True, capture_output=True, text=True) return result.stdout, result.stderr def md_to_pdf(input_md, output_pdf, title="律师业务操作指引"): """使用weasyprint将markdown转为PDF""" html_content = f""" """ # 读取markdown文件 with open(input_md, 'r', encoding='utf-8') as f: md_content = f.read() # 简单的markdown到html转换 lines = md_content.split('\n') in_table = False table_rows = [] html_lines = [] for line in lines: # 跳过YAML front matter if line.strip() == '---': continue # 表格处理 if '|' in line and line.strip().startswith('|'): cells = [c.strip() for c in line.split('|')[1:-1]] # 检查是否是分隔行 if all(re.match(r'^[-:]+$', c.replace(' ', '')) for c in cells if c): continue if not in_table: in_table = True table_rows = [] table_rows.append(cells) continue else: if in_table: in_table = False # 生成表格HTML html_lines.append('') for i, row in enumerate(table_rows): tag = 'th' if i == 0 else 'td' html_lines.append(f'{"".join(f"<{tag}>{c}" for c in row)}') html_lines.append('
') table_rows = [] # 标题 if line.startswith('#### '): html_lines.append(f'

{line[5:]}

') elif line.startswith('### '): html_lines.append(f'

{line[4:]}

') elif line.startswith('## '): html_lines.append(f'

{line[3:]}

') elif line.startswith('# '): html_lines.append(f'

{line[2:]}

') # 粗体 elif line.startswith('**') and line.endswith('**') and line.count('**') == 2: html_lines.append(f'

{line[2:-2]}

') elif '**' in line: parts = re.split(r'\*\*(.+?)\*\*', line) if len(parts) > 1: result = '' for j, part in enumerate(parts): if j % 2 == 1: result += f'{part}' else: result += part html_lines.append(f'

{result}

') # 列表 elif line.startswith('- ') or line.startswith('* '): html_lines.append(f'
  • {line[2:].lstrip("* ")}
  • ') elif re.match(r'^\d+\. ', line): num = re.match(r'^(\d+)\. ', line).group(1) html_lines.append(f'
  • {line[len(num)+2:]}
  • ') # 分隔线 elif line.strip() in ('---', '***', '___'): html_lines.append('
    ') # 空行 elif line.strip() == '': html_lines.append('

     

    ') # 普通段落 else: # 处理行内样式 content = line # 处理链接 content = re.sub(r'\[(.+?)\]\(.+?\)', r'\1', content) html_lines.append(f'

    {content}

    ') html_content += '\n'.join(html_lines) html_content += """ """ # 写入临时HTML temp_html = input_md.with_suffix('.html') with open(temp_html, 'w', encoding='utf-8') as f: f.write(html_content) # 用weasyprint转换 css = """ body { font-family: 'Noto Sans CJK SC', 'WenQuanYi Micro Hei', sans-serif; } """ weasy_cmd = f"weasyprint {temp_html} {output_pdf} 2>&1" stdout, stderr = cmd(weasy_cmd) # 清理临时HTML try: temp_html.unlink() except: pass if os.path.exists(output_pdf): return True, output_pdf else: return False, f"Failed: {stderr[:500]}" print("="*60) print("律师业务操作指引爬取与PDF生成") print("="*60) # 已有上海律协指南列表(从网页分析获得) shanghai_guides = [ # (标题, URL, 分类) ("律师从事关税法律业务操作指引(2025)(试行)", "https://www.lawyers.org.cn/info/1f5e0ac6f5de4ed694bae7bef86f2965", "财税与海关"), ("律师代理医疗科技成果转化业务操作指引(2024)", "https://www.lawyers.org.cn/info/638f41cb8d99433aba1da5b179883147", "医药健康"), ("律师办理公司对外担保业务操作指引(2024)", "https://www.lawyers.org.cn/info/6d01cf4ac44a498bb078c263f43579ee", "公司与商事"), ("律师签发律师函业务操作指引(2021)", "https://www.lawyers.org.cn/info/1fadea825f34420ca1b73bb1c3ce9df8", "民事"), ("律师从事海商海事业务操作指引", "https://www.acla.org.cn/info/dd085166c6ba4f4991a0551944c2cd31", "海事海商"), ("律师办理买卖合同法律事务操作指引", "https://www.acla.org.cn/info/babca008c2ba43e9829b3192ed5f13a9", "民商事"), ("律师办理商业秘密法律业务操作指引(修订版)", "https://www.acla.org.cn/info/c8e2a90999394156a9bf57a459e74299", "知识产权"), ("律师办理专利侵权业务操作指引", "https://www.acla.org.cn/info/505ba22a0d2d4b8fb8c30f63c1192ed7", "知识产权"), ("律师办理婚姻家庭法律业务操作指引", "https://acla.org.cn/info/db55cc94958f4f8eaebeae94e37a77a5", "婚姻家事"), ("律师从事税法服务业务操作指引", "https://www.acla.org.cn/info/6ac4bfc62cdf427a8bb9822bfe3ce1f7", "财税"), ] print(f"准备爬取 {len(shanghai_guides)} 个指引...") # 保存清单 清单 = [] for title, url, category in shanghai_guides: 清单.append({"标题": title, "URL": url, "分类": category, "状态": "待处理"}) 清单文件 = BASE_DIR / "指引清单.json" with open(清单文件, 'w', encoding='utf-8') as f: json.dump(清单, f, ensure_ascii=False, indent=2) print(f"清单已保存到: {清单文件}") print("下一步: 请使用web_fetch工具逐一获取各指引内容")