抓了两个抖音的接口,一个是官方新的解析接口,另一个是拼接测试出来一个未公开的查用户信息的接口,一起分享给大家。
代码放到阿里云的函数计算,可以直接托管到公网(腾讯云的云函数还需要自己配置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)
- Python多线程截图工具 [2024-09-10]
- 密码强度检测Python源代码 [2023-06-06]
- 批量转存百度网盘分享资源Python脚本 [2022-09-29]
- Python一键安装卸载工具V1.0 [2022-07-21]
- Python+HTML随机点名抽奖源码模板 [2021-11-01]