API,仅供学习交流使用,禁止用于商业用途、违法用途等,否则后果自负
前言
闲来无事,研究了一下抖音的视频接口,都需要cookie、msToken、a_bogus....这些参数才能调用获取解析,而且a_bogus算法且复杂,cookie也要时长更新,而我这个能跳过这些参数,直接能解析视频信息出来,无需cookie、a_bogus,且解析的视频一直都不会失效
原理
当我们请求手机端的抖音链接的时候,在F12获取源代码能得到一些视频信息json数据,所以只需要正则取到源代码下面的json数据就行了,请求头只需要添加User-Agent、Referer即可,无需cookie
模拟请求,得到了需要的数据,再使用正则取出转成json数据再取出值就好了
注意
https://www.douyin.com/aweme/v1/play/?video_id=v0200fg10000cqd77sfog65gatpq9nm0 (复制再访问,在这访问会有重定向错误)
该链接是抖音无水印播放接口,且每次访问都会跳转到新的地址,按道理视频一直都不会失效 video_id 参数在页面能搜索到
demo
现在只支持解析图集跟视频,后续会添加实况视频解析
视频: https://api.yujn.cn/api/dy_jx.php?msg=https://v.douyin.com/irC9f9js/
图集: https://api.yujn.cn/api/dy_jx.php?msg=https://v.douyin.com/irC98HWU/
贴上脚本
php
<?php
$msg = urldecode($_REQUEST['msg']); //获取视频链接
if (is_numeric($msg)) {
$video_id = $msg;
} else {
preg_match('/https?:\/\/[^\s]+/', $msg, $video_url);
$video_url = $video_url[0];
$redirected_url = get_redirected_url($video_url);
preg_match('/(\d+)/', $redirected_url, $matches);
$video_id = $matches[1];
// echo $video_id;
}
function get_redirected_url($url) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch);
$redirected_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
curl_close($ch);
return $redirected_url;
}
$headers = [
'User-Agent: Mozilla/5.0 (Linux; Android 8.0.0; SM-G955U Build/R16NW) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Mobile Safari/537.36',
'Referer: https://www.douyin.com/?is_from_mobile_home=1&recommend=1'
];
$url = "https://www.iesdouyin.com/share/video/$video_id/";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
curl_close($ch);
preg_match('/_ROUTER_DATA\s*=\s*(\{.*?\});/', $response, $matches);
$data = $matches[1];
// 解析 JSON 数据
$jsonData = json_decode($data, true);
// 获取视频信息
$itemList = $jsonData['loaderData']['video_(id)/page']['videoInfoRes']['item_list'][0];
$nickname = $itemList['author']['nickname'];
$title = $itemList['desc'];
$awemeId = $itemList['aweme_id'];
$video = $itemList['video']['play_addr']['uri'];
$videoUrl = $video !== null ? (strpos($video, 'mp3') === false ? 'https://www.douyin.com/aweme/v1/play/?video_id=' . $video : $video) : null;
$cover = $itemList['video']['cover']['url_list'][0];
$images = $itemList['images'] ?? null;
$output = [
'msg' =>empty($nickname)?'解析失败!💦':'解析成功!💬️',
'name' => $nickname,
'title' => $title,
// 'aweme_id' => $awemeId,
'video' => $videoUrl,
'cover' => $cover,
'images' => array_map(function($image) {
return $image['url_list'][0];
}, is_array($images) ? $images : []),
'type' =>empty($images)?'视频':'图集',
'tips' => '遇见API api.yujn.cn'
];
header('Content-Type: application/json');
echo json_encode($output,JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
?>
python
#遇见API api.yujn.cn
import requests
import re
import json
headers ={
'user-agent':'Mozilla/5.0 (Linux; Android 8.0.0; SM-G955U Build/R16NW) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Mobile Safari/537.36',
'referer':'https://www.douyin.com/?is_from_mobile_home=1&recommend=1'
}
url ='https://www.iesdouyin.com/share/video/7358413207083486505/'
res =requests.get(url,headers=headers).text
data =re.findall(r'_ROUTER_DATA\s*=\s*(\{.*?\});', res)[0]
json_data =json.loads(data)
# print(data)
item_list =json_data['loaderData']['video_(id)/page']['videoInfoRes']['item_list'][0]
nickname =item_list['author']['nickname']
title =item_list['desc']
aweme_id =item_list['aweme_id']
video =item_list['video']['play_addr']['uri']
video = 'https://www.douyin.com/aweme/v1/play/?video_id=' + video if 'mp3' not in video else video
cover =item_list['video']['cover']['url_list'][0]
img =item_list['images']
print(title)
print(aweme_id)
print(video)
print(cover)
for image in img:
images = image['url_list'][0]
print(images)
#遇见API api.yujn.cn
后续会添加更多东西。。。。