Python抖音官方接口分享
抓了两个抖音的接口,一个是官方新的解析接口,另一个是拼接测试出来一个未公开的查用户信息的接口,一起分享给大家。
代码放到阿里云的函数计算,可以直接托管到公网(腾讯云的云函数还需要自己配置API网关)。
代码
import requests import re headers = { 'User-Agent': 'Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Mobile Safari/537.36' } def user_info(uid): url = 'https://www.iesdouyin.com/web/api/v2/user/info/?uid=' + uid try: r = requests.get(url).json() info = r['user_info'] # [print(i, v) for i, v in info.items() if not v] # 删除null/false return info except Exception: return {'error': '出错了!'} def user_info_url(url): if 'share/user/' in url: id = re.findall(r'share/user/(d+)??', url)[0] else: try: r = requests.head(url, headers=headers, allow_redirects=False) id = re.findall(r'share/user/(d+)??', r.headers['Location'])[0] except Exception: return {'error': 'URL错误!'} return user_info(id) def parse_by_url(url): if '/share/video/' in url: id = re.findall(r'share/video/(d+)/???', url)[0] else: try: r = requests.head(url, headers=headers, allow_redirects=False) id = re.findall(r'share/video/(d+)/???', r.headers['Location'])[0] except Exception: return {'error': 'URL错误!'} return parse_by_id(id) def parse_by_id(id): url = 'https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids=' + id try: r = requests.get(url).json() video = r['item_list'][0] return video except Exception: return {'error': '出错了!'} if __name__ == "__main__": # a = user_info_url('https://v.douyin.com/JBknYQp/') # a = user_info('72673737181') a = parse_by_url('https://v.douyin.com/JBBebxv/') # a = parse_by_id('6808453751930719502') print(a)
本文结束