接入教程
1. 访问Xeno-canto官网查看API文档
2. 使用基础查询参数搜索鸟类录音
3. 获取录音文件URL或元数据JSON
4. 集成到您的鸟类研究或分析应用中
Python 示例:搜索鸟类录音
python
import requests
base_url = 'https://xeno-canto.org/api/2'
params = {
'query': 'gen:Parus',
'page': 1
}
headers = {
'Authorization': 'Bearer YOUR_API_KEY'
}
response = requests.get(base_url + '/recordings', params=params, headers=headers)
data = response.json()
print(f'Found {data["numRecordings"]} recordings')
for recording in data.get('recordings', [])[:3]:
print(f"Species: {recording.get('en')}, Country: {recording.get('cnt')}")
PHP 示例:获取录音详情
php
<?php
$baseUrl = 'https://xeno-canto.org/api/2';
$recordingId = '123456';
$apiKey = 'YOUR_API_KEY';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $baseUrl . '/recordings/' . $recordingId);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer ' . $apiKey
]);
$response = curl_exec($ch);
curl_close($ch);
$data = json_decode($response, true);
if (isset($data['id'])) {
echo "Recording ID: " . $data['id'] . "\n";
echo "Species: " . $data['en'] . "\n";
echo "Audio URL: " . $data['file'] . "\n";
}
?>
JavaScript 示例:按国家筛选录音
javascript
const baseUrl = 'https://xeno-canto.org/api/2';
const country = 'China';
const apiKey = 'YOUR_API_KEY';
async function fetchRecordingsByCountry() {
try {
const response = await fetch(`${baseUrl}/recordings?query=cnt:${country}&page=1`, {
headers: {
'Authorization': `Bearer ${apiKey}`
}
});
const data = await response.json();
console.log(`Recordings from ${country}: ${data.numRecordings}`);
data.recordings.slice(0, 3).forEach(rec => {
console.log(`ID: ${rec.id}, Species: ${rec.en}, Date: ${rec.date}`);
});
} catch (error) {
console.error('Error fetching recordings:', error);
}
}
fetchRecordingsByCountry();
常见问题
Xeno-canto API 是否需要付费?
Xeno-canto API 目前提供免费访问服务,但需遵循其使用条款和限制,例如请求频率限制。商业用途或大规模数据抓取可能需要特殊授权。
API 支持哪些查询参数?
API 支持多种查询参数,如按物种(gen)、国家(cnt)、录音质量(q)等筛选。详细参数请参考官方文档的查询语法部分。
如何获取 API 密钥?
通常无需 API 密钥即可访问公开数据,但某些高级功能或更高请求限额可能需要注册账户并申请密钥。请查阅官网的 API 指南获取最新信息。
Aitishiku.com