API接口-随机图片
1. API接口-随机图片
1.1. 随机图片 API 接口合集
本文收集整理了一系列优质的随机图片 API 接口,可用于网站背景、文章配图等场景。按照不同类别和用途进行整理,包括国际知名 API 和国内优质 API 资源。
1.1.1. 国际知名 API
1.1.1.1. Unsplash Source
- 基本调用格式:
https://source.unsplash.com/random/宽度x高度 - 示例:
https://source.unsplash.com/random/800x600 - 关键词筛选:
https://source.unsplash.com/featured/?nature,water - 防止缓存:
https://source.unsplash.com/random/300x200?sig=${Math.random()} - 特点:高质量摄影作品,支持自定义筛选
- 官网:https://source.unsplash.com/
1.1.1.2. Lorem Picsum
- 基本调用格式:
https://picsum.photos/宽度/高度 - 示例:
https://picsum.photos/800/600 - 获取相同随机图片:
https://picsum.photos/seed/picsum/200/300 - 防止缓存:
https://picsum.photos/200/300?random=1 - 特点:来源于 Unsplash 的高清图片库
- 官网:https://picsum.photos/
1.1.1.3. 特色图片 API
-
Placekitten
- 格式:
https://placekitten.com/宽度/高度 - 示例:
https://placekitten.com/800/600 - 特点:提供随机小猫图片
- 格式:
-
API Ninjas Random Image
- 支持类别:nature, city, technology, food 等
- 支持自定义尺寸(1-5000 之间)
- 需要 API 密钥
- 官网:https://api-ninjas.com/api/randomimage
-
Flickr API
- 基本调用:
https://api.flickr.com/services/rest/?method=flickr.photos.search - 支持多种筛选参数:tags、text、user_id 等
- 需要 API 密钥
- 官网:https://www.flickr.com/services/api/
- 基本调用:
-
Giphy API
- 基本调用:
https://api.giphy.com/v1/gifs/random - 支持 GIF、Sticker 等多种格式
- 提供免费 API 密钥
- 官网:https://developers.giphy.com/
- 基本调用:
-
Pexels API
- RESTful API 接口
- 支持搜索、筛选、随机获取
- 提供免费 API 密钥
- 官网:https://www.pexels.com/api/
1.1.2. 国内优质 API
1.1.2.1. 动漫类
1.1.2.1.1. 樱花 API
- 基本调用:
https://www.dmoe.cc/random.php - JSON 格式:
https://www.dmoe.cc/random.php?return=json - 特点:二次元动漫图片,更新频繁
- 官网:https://www.dmoe.cc/
1.1.2.1.2. 小歪 API
- 二次元动漫:
https://api.ixiaowai.cn/api/api.php - MC 酱动漫:
https://api.ixiaowai.cn/mcapi/mcapi.php - 高清壁纸:
https://api.ixiaowai.cn/gqapi/gqapi.php - 官网:https://api.ixiaowai.cn/
1.1.2.1.3. 姬长信 API
- 每日必应:
https://api.isoyu.com/bing_images.php - 美女图片:
https://api.isoyu.com/mm_images.php - 网红专栏:
https://api.isoyu.com/beibei_images.php - 动态 IP 签名:
https://api.isoyu.com/ip_images.php?signature=早安 - ARU 表情包:
https://api.isoyu.com/ARU_GIF_S.php - 官网:https://api.isoyu.com/
1.1.2.1.4. 其他动漫 API
-
墨天逸
- 接口地址:
https://api.mtyqx.cn/tapi/random.php - 官网:https://api.mtyqx.cn/
- 接口地址:
-
东方 Project
- 接口地址:
https://img.paulzzh.tech/touhou/random - 官网:https://img.paulzzh.tech/
- 接口地址:
-
动漫星空
- 接口地址:
https://api.dongmanxingkong.com/suijitupian/acg/1080p/index.php
- 接口地址:
-
樱花 API
- 接口地址:
https://www.dmoe.cc/random.php - 支持 JSON 格式返回
- 稳定性高,更新及时
- 接口地址:
-
Lolicon API
- 接口地址:
https://api.lolicon.app/setu/v2 - 支持 R18 分级
- 需要 API Key
- 官网:https://api.lolicon.app
- 接口地址:
1.1.2.2. 综合类
1.1.2.2.1. 搏天 API
- 基本调用:
https://api.btstu.cn/sjbz/api.php - 手机动漫:
http://api.btstu.cn/sjbz/?lx=m_dongman - 电脑动漫:
http://api.btstu.cn/sjbz/?lx=dongman - 官网:https://api.btstu.cn/doc/sjbz.php
1.1.2.2.2. 其他综合 API
-
岁月小筑
- 接口地址:
https://img.xjh.me/random_img.php - 官网:https://img.xjh.me/
- 接口地址:
-
EEE.DOG
- 接口地址:
https://api.yimian.xyz/img - 特点:基于华为云存储,全球 CDN 加速
- 图片类型:二次元、必应壁纸、头像等
- 官网:https://www.eee.dog/tech/rand-pic-api.html
- 接口地址:
-
夏沫博客
- 古风美图:
https://cdn.seovx.com/?mom=302 - 二次元图:
https://cdn.seovx.com/d/?mom=302 - 随机图片:
https://cdn.seovx.com/ha/?mom=302 - 官网:https://cdn.seovx.com/
- 古风美图:
1.1.3. API 使用示例
1.1.3.1. JavaScript 调用示例
// Unsplash随机图片
async function getRandomImage() {
const width = 800;
const height = 600;
const url = `https://source.unsplash.com/random/${width}x${height}?sig=${Math.random()}`;
return url;
}
// Lorem Picsum带缓存控制
async function getPicsumImage() {
const url = `https://picsum.photos/800/600?random=${Date.now()}`;
return url;
}
// 动漫图片API调用
async function getAnimeImage() {
try {
const response = await fetch('https://www.dmoe.cc/random.php?return=json');
const data = await response.json();
return data.imgurl;
} catch (error) {
console.error('获取图片失败:', error);
return null;
}
}
1.1.3.2. Python 调用示例
import requests
# Unsplash图片获取
def get_unsplash_image():
width, height = 800, 600
url = f'https://source.unsplash.com/random/{width}x{height}'
response = requests.get(url)
return response.url
# 动漫API调用
def get_anime_image():
try:
response = requests.get('https://api.ixiaowai.cn/api/api.php')
return response.url
except Exception as e:
print(f'获取图片失败: {e}')
return None
1.1.4. 使用建议
-
缓存处理
- 在 URL 后添加随机参数:
?random=1或?sig=${Math.random()} - 本地做好图片缓存处理
- 在 URL 后添加随机参数:
-
性能优化
- 选择合适的图片尺寸
- 使用 webp 等现代图片格式
- 配合 CDN 加速
-
可靠性保障
- 选择稳定性高的 API
- 设置多个备用 API
- 重要项目建议自建
-
使用规范
- 遵守 API 使用限制和规则
- 注意版权问题
- 控制请求频率