宝可梦图鉴 API 文档

简介

欢迎使用宝可梦图鉴 API!这个 API 可以让你通过编程方式获取宝可梦数据。下面是所有可用的端点及其使用方式。这些端点展示了不同的认证方法和请求类型,非常适合用于学习爬虫技术和 HTTP 协议。

基础 URL

所有 API 请求都应该使用以下基础 URL:

http://pokedex.hlqeai.cn/api

端点

GET /pokemon

获取所有宝可梦的列表。需要 API 密钥认证。

请求头

X-API-Key: pokemon123

查询参数

  • limit (可选) - 限制返回的宝可梦数量
  • offset (可选) - 跳过前 N 个宝可梦,默认为 0
  • type (可选) - 按属性筛选宝可梦,可以通过多种方式筛选多个属性:
    • type=火 - 筛选火属性
    • type=火&type=格斗 - 筛选同时具有火和格斗属性的宝可梦
    • type=火&格斗 - 筛选同时具有火和格斗属性的宝可梦(简写方式)

示例请求

curl -H "X-API-Key: pokemon123" http://pokedex.hlqeai.cn/api/pokemon?limit=5&type=火
curl -H "X-API-Key: pokemon123" http://pokedex.hlqeai.cn/api/pokemon?type=火&格斗

示例响应

[ { "id": 4, "name": "小火龙", "types": ["火"], ... }, ... ]

GET /pokemon/{id}

获取指定 ID 的宝可梦详细信息。需要 Cookie 认证。

请求 Cookie

auth_token=poke_session123

路径参数

  • id - 宝可梦的 ID

示例请求

curl --cookie "auth_token=poke_session123" http://pokedex.hlqeai.cn/api/pokemon/25

示例响应

{ "id": 25, "name": "皮卡丘", "types": ["电"], "abilities": ["静电"], ... }

POST /pokemon/search

搜索宝可梦。必须使用 JSON 格式发送请求体。

请求头

Content-Type: application/json

请求体

{ "name": "妙", // 可选,按名称搜索 "type": "草", // 可选,按属性搜索 "id": 1 // 可选,按ID搜索 }

示例请求

curl -X POST -H "Content-Type: application/json" -d '{"name":"妙","type":"草"}' http://pokedex.hlqeai.cn/api/pokemon/search

示例响应

[ { "id": 1, "name": "妙蛋种子", "types": ["草", "毒"], ... }, ... ]

应用到爬虫学习的要点

  1. HTTP 请求方法: API 展示了 GET 和 POST 两种常用请求方法的使用方式。
  2. 请求头定制: 不同的端点需要不同的请求头,如 X-API-KeyContent-Type
  3. Cookie 认证: 了解如何通过 Cookie 进行认证。
  4. 参数传递: 包括查询参数、路径参数和请求体参数的使用。
  5. JSON 处理: 学习如何发送和解析 JSON 格式数据。

Python 爬虫示例代码

以下是使用 Python 爬取我们 API 的简单示例:

1. 使用 GET 请求获取宝可梦列表

```python import requests # GET请求示例 # 注意定制请求头中的API密钥 headers = { 'X-API-Key': 'pokemon123' } # 添加查询参数 params = { 'limit': 10, 'type': '火' } response = requests.get('http://pokedex.hlqeai.cn/api/pokemon', headers=headers, params=params) if response.status_code == 200: pokemons = response.json() for pokemon in pokemons: print(f"{pokemon['id']} - {pokemon['name']}") else: print(f"Error: {response.status_code}") ```

2. 使用 Cookie 认证获取单个宝可梦

```python import requests # 设置Cookie cookies = { 'auth_token': 'poke_session123' } pokemon_id = 25 # 皮卡丘 response = requests.get(f'http://pokedex.hlqeai.cn/api/pokemon/{pokemon_id}', cookies=cookies) if response.status_code == 200: pokemon = response.json() print(f"Name: {pokemon['name']}") print(f"Types: {', '.join(pokemon['types'])}") else: print(f"Error: {response.status_code}") ```

3. 使用 POST 请求搜索宝可梦

```python import requests import json # 设置请求头 headers = { 'Content-Type': 'application/json' } # 准备请求体 data = { 'name': '妙', 'type': '草' } response = requests.post( 'http://pokedex.hlqeai.cn/api/pokemon/search', headers=headers, data=json.dumps(data) ) if response.status_code == 200: results = response.json() print(f"Found {len(results)} Pokemon:") for pokemon in results: print(f"{pokemon['id']} - {pokemon['name']}") else: print(f"Error: {response.status_code}") ```

4. BeautifulSoup 解析网页示例

```python import requests from bs4 import BeautifulSoup # 获取网页内容 response = requests.get('http://pokedex.hlqeai.cn/') html_content = response.text # 创建BeautifulSoup对象 soup = BeautifulSoup(html_content, 'html.parser') # 提取所有宝可梦卡片 pokemon_cards = soup.find_all('div', class_='pokemon-card') for card in pokemon_cards: # 提取宝可梦ID pokemon_id = card.find('div', class_='pokemon-id').text.strip('#') # 提取宝可梦名称 pokemon_name = card.find('h3', class_='pokemon-name').text # 提取宝可梦属性 types = [span.text for span in card.find_all('span', class_='type-badge')] # 提取详情页链接 detail_link = card.find('a', class_='btn').get('href') print(f"ID: {pokemon_id}, Name: {pokemon_name}") print(f"Types: {', '.join(types)}") print(f"Detail Link: {detail_link}") print("---") ```