Files
ai-contract-platform/scripts/test-post-search-api.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

166 lines
4.6 KiB
Python

#!/usr/bin/env python3
"""
使用POST搜索API获取法律列表和bbbs ID
根据API文档,POST /law-search/search/list 可能可以获取完整的法律列表包含bbbs
"""
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"
def post_search_laws(title: str = "", page: int = 1, size: int = 20, category_id: int = None):
"""使用POST API搜索法律"""
url = f"{API_BASE}/search/list"
# 构建请求体
body = {
"title": title,
"page": page,
"size": size
}
if category_id:
body["flflId"] = category_id # 分类ID
data = json.dumps(body).encode('utf-8')
req = urllib.request.Request(
url,
data=data,
headers={
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
'Accept': 'application/json',
'Content-Type': 'application/json;charset=UTF-8',
'Referer': 'https://flk.npc.gov.cn/'
},
method='POST'
)
try:
with urllib.request.urlopen(req, context=ssl_context, timeout=30) as response:
return json.loads(response.read().decode('utf-8'))
except Exception as e:
print(f"POST request failed: {e}")
return None
def search_by_category(category_id: int, category_name: str, max_pages: int = 20):
"""按分类搜索获取所有法律"""
print(f"\nSearching category: {category_name} (id={category_id})")
all_laws = []
for page in range(1, max_pages + 1):
print(f" Page {page}...")
result = post_search_laws(category_id=category_id, page=page, size=100)
if not result or result.get('code') != 200:
print(f" Failed at page {page}")
break
data = result.get('data', {})
items = data.get('rows', [])
if not items:
print(f" No more items")
break
for item in items:
all_laws.append({
'bbbs': item.get('bbbs'),
'title': item.get('title'),
'type': item.get('flxz'),
'authority': item.get('zdjgName'),
'publish_date': item.get('gbrq'),
'effective_date': item.get('sxrq')
})
total = data.get('total', 0)
print(f" Found {len(items)} items, total={total}")
if len(all_laws) >= total:
break
time.sleep(0.5)
return all_laws
def search_specific_law(title: str):
"""搜索特定法律并获取bbbs"""
print(f"\nSearching: {title}")
result = post_search_laws(title=title, page=1, size=10)
if not result or result.get('code') != 200:
print(f" Search failed")
return None
data = result.get('data', {})
items = data.get('rows', [])
# 精确匹配
for item in items:
if item.get('title') == title:
print(f" Found exact match: bbbs={item.get('bbbs')}")
return {
'bbbs': item.get('bbbs'),
'title': item.get('title'),
'type': item.get('flxz'),
'authority': item.get('zdjgName'),
'publish_date': item.get('gbrq'),
'effective_date': item.get('sxrq')
}
# 返回最接近的匹配
if items:
print(f" No exact match, closest: {items[0].get('title')}")
return None
return None
def main():
print("=" * 60)
print("Testing POST search API for bbbs ID")
print("=" * 60)
# 测试POST搜索API
# 先尝试搜索特定法律
test_titles = [
"中华人民共和国社会保险法",
"中华人民共和国建筑法",
"中华人民共和国合伙企业法",
"工伤保险条例",
]
results = []
for title in test_titles:
law = search_specific_law(title)
if law:
results.append(law)
time.sleep(0.5)
# 输出结果
output = {
"search_date": datetime.now().isoformat(),
"method": "POST_search_api",
"results": results
}
with open('project-docs/phase14-legal-fulltext-library/bbbs-lookup-test.json', 'w', encoding='utf-8') as f:
json.dump(output, f, ensure_ascii=False, indent=2)
print(f"\n\nResults saved to: project-docs/phase14-legal-fulltext-library/bbbs-lookup-test.json")
print(f"Found {len(results)} laws with bbbs")
return results
if __name__ == '__main__':
main()