Spaces:
Sleeping
Sleeping
| #!/usr/bin/env python3 | |
| """ | |
| Crossref API 调试脚本 | |
| 逐步测试不同的参数组合来找出问题 | |
| """ | |
| import requests | |
| import json | |
| def test_basic_search(): | |
| """测试最基本的搜索""" | |
| url = "https://api.crossref.org/works" | |
| headers = { | |
| 'User-Agent': 'Academic-Reviewer-System/1.0 (mailto:[email protected])' | |
| } | |
| # 最基本的参数 | |
| params = { | |
| "query": "machine learning", | |
| "rows": 3 | |
| } | |
| print("=== 测试1: 基本搜索 ===") | |
| print(f"URL: {url}") | |
| print(f"参数: {params}") | |
| print("-" * 50) | |
| try: | |
| response = requests.get(url, params=params, headers=headers, timeout=30) | |
| print(f"状态码: {response.status_code}") | |
| if response.status_code == 200: | |
| data = response.json() | |
| items = data.get("message", {}).get("items", []) | |
| print(f"成功! 返回 {len(items)} 个结果") | |
| for i, item in enumerate(items, 1): | |
| title = item.get('title', ['N/A'])[0] if item.get('title') else 'N/A' | |
| print(f"{i}. {title}") | |
| return True | |
| else: | |
| print(f"失败: {response.text}") | |
| return False | |
| except Exception as e: | |
| print(f"异常: {str(e)}") | |
| return False | |
| def test_with_sort(): | |
| """测试添加排序参数""" | |
| url = "https://api.crossref.org/works" | |
| headers = { | |
| 'User-Agent': 'Academic-Reviewer-System/1.0 (mailto:[email protected])' | |
| } | |
| # 添加排序参数 | |
| params = { | |
| "query": "machine learning", | |
| "rows": 3, | |
| "sort": "published", | |
| "order": "desc" | |
| } | |
| print("\n=== 测试2: 添加排序参数 ===") | |
| print(f"参数: {params}") | |
| print("-" * 50) | |
| try: | |
| response = requests.get(url, params=params, headers=headers, timeout=30) | |
| print(f"状态码: {response.status_code}") | |
| if response.status_code == 200: | |
| data = response.json() | |
| items = data.get("message", {}).get("items", []) | |
| print(f"成功! 返回 {len(items)} 个结果") | |
| return True | |
| else: | |
| print(f"失败: {response.text}") | |
| return False | |
| except Exception as e: | |
| print(f"异常: {str(e)}") | |
| return False | |
| def test_with_select(): | |
| """测试添加select参数""" | |
| url = "https://api.crossref.org/works" | |
| headers = { | |
| 'User-Agent': 'Academic-Reviewer-System/1.0 (mailto:[email protected])' | |
| } | |
| # 添加select参数 | |
| params = { | |
| "query": "machine learning", | |
| "rows": 3, | |
| "select": "DOI,title,author" | |
| } | |
| print("\n=== 测试3: 添加select参数 ===") | |
| print(f"参数: {params}") | |
| print("-" * 50) | |
| try: | |
| response = requests.get(url, params=params, headers=headers, timeout=30) | |
| print(f"状态码: {response.status_code}") | |
| if response.status_code == 200: | |
| data = response.json() | |
| items = data.get("message", {}).get("items", []) | |
| print(f"成功! 返回 {len(items)} 个结果") | |
| return True | |
| else: | |
| print(f"失败: {response.text}") | |
| return False | |
| except Exception as e: | |
| print(f"异常: {str(e)}") | |
| return False | |
| def test_cited_count_sort(): | |
| """测试按引用量排序""" | |
| url = "https://api.crossref.org/works" | |
| headers = { | |
| 'User-Agent': 'Academic-Reviewer-System/1.0 (mailto:[email protected])' | |
| } | |
| # 测试不同的排序选项 | |
| sort_options = ["published", "relevance", "deposited"] | |
| for sort_option in sort_options: | |
| params = { | |
| "query": "machine learning", | |
| "rows": 3, | |
| "sort": sort_option, | |
| "order": "desc" | |
| } | |
| print(f"\n=== 测试4: 排序选项 '{sort_option}' ===") | |
| print(f"参数: {params}") | |
| print("-" * 50) | |
| try: | |
| response = requests.get(url, params=params, headers=headers, timeout=30) | |
| print(f"状态码: {response.status_code}") | |
| if response.status_code == 200: | |
| data = response.json() | |
| items = data.get("message", {}).get("items", []) | |
| print(f"成功! 返回 {len(items)} 个结果") | |
| # 显示引用量信息 | |
| for i, item in enumerate(items, 1): | |
| title = item.get('title', ['N/A'])[0] if item.get('title') else 'N/A' | |
| cited_count = item.get('is-referenced-by-count', 0) | |
| print(f"{i}. {title} (被引用: {cited_count})") | |
| else: | |
| print(f"失败: {response.text}") | |
| except Exception as e: | |
| print(f"异常: {str(e)}") | |
| def test_full_params(): | |
| """测试完整参数""" | |
| url = "https://api.crossref.org/works" | |
| headers = { | |
| 'User-Agent': 'Academic-Reviewer-System/1.0 (mailto:[email protected])' | |
| } | |
| # 完整的参数 | |
| params = { | |
| "query": "machine learning", | |
| "rows": 3, | |
| "sort": "published", | |
| "order": "desc", | |
| "select": "DOI,title,author,container-title,published-print,published-online,is-referenced-by-count" | |
| } | |
| print(f"\n=== 测试5: 完整参数 ===") | |
| print(f"参数: {params}") | |
| print("-" * 50) | |
| try: | |
| response = requests.get(url, params=params, headers=headers, timeout=30) | |
| print(f"状态码: {response.status_code}") | |
| if response.status_code == 200: | |
| data = response.json() | |
| items = data.get("message", {}).get("items", []) | |
| print(f"成功! 返回 {len(items)} 个结果") | |
| for i, item in enumerate(items, 1): | |
| title = item.get('title', ['N/A'])[0] if item.get('title') else 'N/A' | |
| cited_count = item.get('is-referenced-by-count', 0) | |
| print(f"{i}. {title} (被引用: {cited_count})") | |
| return True | |
| else: | |
| print(f"失败: {response.text}") | |
| return False | |
| except Exception as e: | |
| print(f"异常: {str(e)}") | |
| return False | |
| if __name__ == "__main__": | |
| print("Crossref API 调试测试") | |
| print("=" * 60) | |
| # 逐步测试 | |
| test_basic_search() | |
| test_with_sort() | |
| test_with_select() | |
| test_cited_count_sort() | |
| test_full_params() | |
| print("\n" + "=" * 60) | |
| print("调试完成") | |