Files
ai-contract-platform/scripts/build-priority1-law-list.py
T
yuqei 50cc7cb1cf feat: complete Phase14 legal library expansion
- Import 789,853 legal articles from LawRefBook/Laws
- Add 19,712 laws including national and regional regulations
- Filter 1,370 expired laws
- Create vectorization scripts and backup tools
- Add law update monitoring mechanism
- Generate comprehensive documentation

Database: PostgreSQL with GIN index for full-text search
Vectorization: 100% complete with LegalSemanticMatcher
Performance: 7x improvement in semantic search

Co-Authored-By: Claude <noreply@anthropic.com>
2026-06-16 21:26:20 +08:00

205 lines
16 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
"""
批量搜索国家法律法规数据库获取法律的bbbs ID
策略:
1. 使用搜索建议API获取搜索结果
2. 从热门下载列表获取部分bbbs
3. 手动补充已知法律的bbbs(从已有清单或手动搜索)
最终输出:包含bbbs ID的优先级一法律清单,用于生成导入batch文件
"""
import json
import time
import urllib.parse
import urllib.request
import ssl
from datetime import datetime
ssl_context = ssl.create_default_context()
ssl_context.check_hostname = False
ssl_context.verify_mode = ssl.CERT_NONE
API_BASE = "https://flk.npc.gov.cn/law-search"
# 优先级一法律清单 - 包含手动补充的bbbs ID
PRIORITY1_LAWS_WITH_BBBS = [
# 已导入的法律(从现有清单获取)
{"title": "中华人民共和国民法典", "short_name": "民法典", "bbbs": "ff808081729d1efe01729d50b5c500bf", "category": "law", "status": "already_imported"},
{"title": "中华人民共和国电子签名法", "short_name": "电子签名法", "bbbs": "ff8080816f135f46016f2163e4261aa1", "category": "law", "status": "already_imported"},
{"title": "中华人民共和国个人信息保护法", "short_name": "PIPL", "bbbs": "ff8081817b6472a3017b656cc2040044", "category": "law", "status": "already_imported"},
{"title": "中华人民共和国劳动合同法", "short_name": "劳动合同法", "bbbs": "2c909fdd678bf17901678bf74d7106b3", "category": "law", "status": "already_imported"},
{"title": "中华人民共和国数据安全法", "short_name": "数据安全法", "bbbs": "ff80818179f5e0800179f885c7e70392", "category": "law", "status": "already_imported"},
{"title": "中华人民共和国公司法", "short_name": "公司法", "bbbs": "ff8081818c9108eb018cb6922f750c07", "category": "law", "status": "already_imported"},
# 合同通用司法解释 - 需手动查找bbbs
{"title": "最高人民法院关于适用《中华人民共和国民法典》合同编通则若干问题的解释", "short_name": "民法典合同编通则司法解释", "category": "judicial_interpretation", "priority": 1, "knowledge_domain": "contract-general", "bbbs": None, "notes": "法释〔2023〕13号,2023年12月5日施行"},
{"title": "最高人民法院关于适用《中华人民共和国民法典》有关担保制度的解释", "short_name": "民法典担保制度解释", "category": "judicial_interpretation", "priority": 1, "knowledge_domain": "guarantee", "bbbs": None, "notes": "法释〔2020〕28号,2021年1月1日施行"},
# 劳动用工
{"title": "中华人民共和国劳动合同法实施条例", "short_name": "劳动合同法实施条例", "category": "administrative_regulation", "priority": 1, "knowledge_domain": "labor-employment", "bbbs": None, "notes": "国务院令第535号,2008年9月18日施行"},
{"title": "最高人民法院关于审理劳动争议案件适用法律问题的解释(一)", "short_name": "劳动争议司法解释", "category": "judicial_interpretation", "priority": 1, "knowledge_domain": "labor-employment", "bbbs": None, "notes": "法释〔2020〕26号,2021年1月1日施行"},
{"title": "中华人民共和国社会保险法", "short_name": "社会保险法", "category": "law", "priority": 1, "knowledge_domain": "labor-employment", "bbbs": None, "notes": "2011年7月1日施行"},
{"title": "工伤保险条例", "short_name": "工伤保险条例", "category": "administrative_regulation", "priority": 1, "knowledge_domain": "labor-employment", "bbbs": None, "notes": "国务院令第586号,2011年修订施行"},
{"title": "中华人民共和国劳动法", "short_name": "劳动法", "category": "law", "priority": 1, "knowledge_domain": "labor-employment", "bbbs": None, "notes": "1995年1月1日施行,已规划"},
# 采购招投标 - 已规划
{"title": "中华人民共和国招标投标法", "short_name": "招投标法", "category": "law", "priority": 1, "knowledge_domain": "procurement-tendering", "bbbs": None, "notes": "已规划LAW-TENDER-001"},
{"title": "中华人民共和国招标投标法实施条例", "short_name": "招投标法实施条例", "category": "administrative_regulation", "priority": 1, "knowledge_domain": "procurement-tendering", "bbbs": None, "notes": "国务院令第613号"},
{"title": "中华人民共和国政府采购法", "short_name": "政府采购法", "category": "law", "priority": 1, "knowledge_domain": "procurement-tendering", "bbbs": None, "notes": "已规划LAW-PROCUREMENT-001"},
{"title": "中华人民共和国政府采购法实施条例", "short_name": "政府采购法实施条例", "category": "administrative_regulation", "priority": 1, "knowledge_domain": "procurement-tendering", "bbbs": None, "notes": "国务院令第658号"},
# 建设工程
{"title": "中华人民共和国建筑法", "short_name": "建筑法", "category": "law", "priority": 1, "knowledge_domain": "construction", "bbbs": None, "notes": "1998年3月1日施行"},
{"title": "建设工程质量管理条例", "short_name": "建设工程质量管理条例", "category": "administrative_regulation", "priority": 1, "knowledge_domain": "construction", "bbbs": None, "notes": "国务院令第279号"},
{"title": "建设工程安全生产管理条例", "short_name": "建设工程安全生产管理条例", "category": "administrative_regulation", "priority": 1, "knowledge_domain": "construction", "bbbs": None, "notes": "国务院令第393号"},
{"title": "最高人民法院关于审理建设工程施工合同纠纷案件适用法律问题的解释(一)", "short_name": "建设工程司法解释", "category": "judicial_interpretation", "priority": 1, "knowledge_domain": "construction", "bbbs": None, "notes": "法释〔2020〕25号"},
# 知识产权 - 已规划
{"title": "中华人民共和国著作权法", "short_name": "著作权法", "category": "law", "priority": 1, "knowledge_domain": "intellectual-property", "bbbs": None, "notes": "已规划LAW-IP-001"},
{"title": "中华人民共和国著作权法实施条例", "short_name": "著作权法实施条例", "category": "administrative_regulation", "priority": 1, "knowledge_domain": "intellectual-property", "bbbs": None, "notes": "国务院令第633号"},
{"title": "信息网络传播权保护条例", "short_name": "信息网络传播权保护条例", "category": "administrative_regulation", "priority": 1, "knowledge_domain": "intellectual-property", "bbbs": None, "notes": "国务院令第468号"},
{"title": "中华人民共和国专利法", "short_name": "专利法", "category": "law", "priority": 1, "knowledge_domain": "intellectual-property", "bbbs": None, "notes": "已规划LAW-IP-002"},
{"title": "中华人民共和国商标法", "short_name": "商标法", "category": "law", "priority": 1, "knowledge_domain": "intellectual-property", "bbbs": None, "notes": "已规划LAW-IP-003"},
{"title": "最高人民法院关于审理技术合同纠纷案件适用法律若干问题的解释", "short_name": "技术合同司法解释", "category": "judicial_interpretation", "priority": 1, "knowledge_domain": "intellectual-property", "bbbs": None, "notes": "已规划LAW-TECH-001"},
# 借款担保
{"title": "最高人民法院关于审理民间借贷案件适用法律若干问题的规定", "short_name": "民间借贷司法解释", "category": "judicial_interpretation", "priority": 1, "knowledge_domain": "loan", "bbbs": None, "notes": "已规划LAW-LOAN-001"},
# 商事主体
{"title": "中华人民共和国合伙企业法", "short_name": "合伙企业法", "category": "law", "priority": 1, "knowledge_domain": "company-commercial", "bbbs": None, "notes": "1997年施行,2007年修订"},
{"title": "中华人民共和国企业破产法", "short_name": "企业破产法", "category": "law", "priority": 1, "knowledge_domain": "company-commercial", "bbbs": None, "notes": "2007年6月1日施行"},
{"title": "中华人民共和国市场主体登记管理条例", "short_name": "市场主体登记管理条例", "category": "administrative_regulation", "priority": 1, "knowledge_domain": "company-commercial", "bbbs": None, "notes": "已规划LAW-MARKET-REGISTRATION-001"},
# 投融资
{"title": "中华人民共和国信托法", "short_name": "信托法", "category": "law", "priority": 1, "knowledge_domain": "investment-finance", "bbbs": None, "notes": "2001年10月1日施行"},
{"title": "中华人民共和国证券法", "short_name": "证券法", "category": "law", "priority": 1, "knowledge_domain": "investment-finance", "bbbs": None, "notes": "2020年修订施行"},
{"title": "中华人民共和国证券投资基金法", "short_name": "证券投资基金法", "category": "law", "priority": 1, "knowledge_domain": "investment-finance", "bbbs": None, "notes": "2015年修订"},
{"title": "中华人民共和国外商投资法", "short_name": "外商投资法", "category": "law", "priority": 1, "knowledge_domain": "investment-finance", "bbbs": None, "notes": "已规划LAW-FOREIGN-INVESTMENT-001"},
{"title": "中华人民共和国反垄断法", "short_name": "反垄断法", "category": "law", "priority": 1, "knowledge_domain": "investment-finance", "bbbs": None, "notes": "已规划LAW-ANTIMONOPOLY-001"},
# 数据网络安全 - 已规划
{"title": "中华人民共和国网络安全法", "short_name": "网络安全法", "category": "law", "priority": 1, "knowledge_domain": "data-privacy", "bbbs": None, "notes": "已规划LAW-DATA-003"},
{"title": "关键信息基础设施安全保护条例", "short_name": "关键信息基础设施安全保护条例", "category": "administrative_regulation", "priority": 1, "knowledge_domain": "data-privacy", "bbbs": None, "notes": "国务院令第745号"},
{"title": "数据出境安全评估办法", "short_name": "数据出境安全评估办法", "category": "departmental_rule", "priority": 1, "knowledge_domain": "data-privacy", "bbbs": None, "notes": "国家网信办,不在国家法律法规数据库"},
{"title": "个人信息出境标准合同办法", "short_name": "个人信息出境标准合同办法", "category": "departmental_rule", "priority": 1, "knowledge_domain": "data-privacy", "bbbs": None, "notes": "国家网信办,不在国家法律法规数据库"},
# 房地产
{"title": "中华人民共和国城市房地产管理法", "short_name": "城市房地产管理法", "category": "law", "priority": 1, "knowledge_domain": "real-estate", "bbbs": None, "notes": "1995年施行"},
{"title": "中华人民共和国土地管理法", "short_name": "土地管理法", "category": "law", "priority": 1, "knowledge_domain": "real-estate", "bbbs": None, "notes": "2020年修订"},
{"title": "最高人民法院关于审理商品房买卖合同纠纷案件适用法律若干问题的解释", "short_name": "商品房买卖司法解释", "category": "judicial_interpretation", "priority": 1, "knowledge_domain": "real-estate", "bbbs": None, "notes": "2003年施行"},
# 争议解决 - 已规划
{"title": "中华人民共和国仲裁法", "short_name": "仲裁法", "category": "law", "priority": 1, "knowledge_domain": "company-commercial", "bbbs": None, "notes": "已规划LAW-ARBITRATION-001"},
{"title": "中华人民共和国民事诉讼法", "short_name": "民事诉讼法", "category": "law", "priority": 1, "knowledge_domain": "company-commercial", "bbbs": None, "notes": "已规划LAW-CIVIL-PROCEDURE-001"},
{"title": "中华人民共和国人民调解法", "short_name": "人民调解法", "category": "law", "priority": 1, "knowledge_domain": "dispute-resolution", "bbbs": None, "notes": "2011年1月1日施行"},
# 商事合作 - 已规划
{"title": "中华人民共和国反不正当竞争法", "short_name": "反不正当竞争法", "category": "law", "priority": 1, "knowledge_domain": "company-commercial", "bbbs": None, "notes": "已规划LAW-COMMERCIAL-001,2025年修订"},
{"title": "中华人民共和国电子商务法", "short_name": "电子商务法", "category": "law", "priority": 1, "knowledge_domain": "company-commercial", "bbbs": None, "notes": "已规划LAW-E-COMMERCE-001"},
{"title": "中华人民共和国消费者权益保护法", "short_name": "消费者权益保护法", "category": "law", "priority": 1, "knowledge_domain": "company-commercial", "bbbs": None, "notes": "已规划LAW-MARKET-001"},
{"title": "商业特许经营管理条例", "short_name": "商业特许经营管理条例", "category": "administrative_regulation", "priority": 1, "knowledge_domain": "franchise", "bbbs": None, "notes": "已规划LAW-FRANCHISE-001"},
# 物业服务 - 已规划
{"title": "物业管理条例", "short_name": "物业管理条例", "category": "administrative_regulation", "priority": 1, "knowledge_domain": "real-estate", "bbbs": None, "notes": "已规划LAW-PROPERTY-MANAGEMENT-001"},
]
def fetch_json(url: str, max_retries: int = 3) -> dict:
"""发送HTTP请求获取JSON响应"""
for attempt in range(max_retries):
try:
req = urllib.request.Request(
url,
headers={
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
'Accept': 'application/json',
'Referer': 'https://flk.npc.gov.cn/'
}
)
with urllib.request.urlopen(req, context=ssl_context, timeout=30) as response:
return json.loads(response.read().decode('utf-8'))
except Exception as e:
if attempt < max_retries - 1:
time.sleep(1)
return None
def get_aggregate_data() -> dict:
"""获取统计数据"""
url = f"{API_BASE}/index/aggregateData"
return fetch_json(url)
def get_law_details(bbbs: str) -> dict:
"""获取法律详情"""
encoded = urllib.parse.quote(bbbs)
url = f"{API_BASE}/search/flfgDetails?bbbs={encoded}"
response = fetch_json(url)
if response and response.get('code') == 200:
return response.get('data')
return None
def main():
print("=" * 60)
print("Building Priority1 Law List with BBBS IDs")
print("=" * 60)
# 获取统计数据中的热门法律
aggregate = get_aggregate_data()
popular_bbbs = {}
if aggregate:
for item in aggregate.get('popularSearch', []):
popular_bbbs[item.get('title')] = item.get('bbbs')
for item in aggregate.get('popularDownload', []):
popular_bbbs[item.get('title')] = item.get('bbbs')
print(f"Popular laws found in aggregate data: {len(popular_bbbs)}")
# 检查哪些优先级一法律在热门列表中
found_count = 0
need_lookup_count = 0
for law in PRIORITY1_LAWS_WITH_BBBS:
title = law['title']
if law.get('bbbs'):
found_count += 1
print(f"[OK] {law['short_name']}: bbbs={law['bbbs']}")
elif title in popular_bbbs:
law['bbbs'] = popular_bbbs[title]
found_count += 1
print(f"[OK] {law['short_name']}: found in popular list, bbbs={law['bbbs']}")
else:
need_lookup_count += 1
print(f"[NEED] {law['short_name']}: needs bbbs lookup")
# 输出结果
output = {
"generated_at": datetime.now().isoformat(),
"source_system": "国家法律法规数据库",
"total_laws": len(PRIORITY1_LAWS_WITH_BBBS),
"found_with_bbbs": found_count,
"needs_lookup": need_lookup_count,
"laws": PRIORITY1_LAWS_WITH_BBBS,
"popular_bbbs_from_aggregate": popular_bbbs
}
output_file = 'project-docs/phase14-legal-fulltext-library/priority1-law-list-with-bbbs.json'
with open(output_file, 'w', encoding='utf-8') as f:
json.dump(output, f, ensure_ascii=False, indent=2)
print("\n" + "=" * 60)
print(f"Total: {len(PRIORITY1_LAWS_WITH_BBBS)} laws")
print(f"Found with bbbs: {found_count}")
print(f"Need lookup: {need_lookup_count}")
print(f"\nOutput: {output_file}")
print("=" * 60)
return output
if __name__ == '__main__':
main()