仅供学习参考,请勿用于非法目的,如有侵权请联系我删除。

获取抖音作品详情

接口分析

获取视频信息,切入点是抖音的分享链接

随便找一个视频,获取分享链接

1
尝试臭豆腐肥肠火锅?这味道简直太太太上头了!!%vlog日常 %美食探店 %火锅 %深夜放毒 %美食趣胃计划  https://v.douyin.com/eNBofpr/ 椱制此链接,打开Dou吟搜索,直接觀看视频!

获取到链接:https://v.douyin.com/eNBofpr/ ,后续便以此为测试数据。

打开Chrome调试模式,很轻松的能定位到实际访问的数据接口:https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids=6942073391922842894

其中item_ids对应的就是本视频的ID:6942073391922842894,眼尖一点就能发现,这个ID出现在了重定向后的Url中。

下面就简单了,整理一下步骤:

  1. 获取分享链接
  2. 访问分享链接
  3. 通过重定向后的分享链接获取作品实际ID
  4. 调用API接口,获取作品信息

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import re
import requests

from urllib import parse

# 关闭烦人提示
requests.packages.urllib3.disable_warnings()

def handle_share_content(share_content: str) -> str:
"""
从App端分享信息中提取分享作品短链接
:param share_content:
:return:
"""
url_regular = re.compile(r"(https|http)?:\/\/(([0-9]{1,3}\.){3}[0-9]{1,3}|([0-9a-zA-Z_!~*'()-]+\.)*([0-9a-zA-Z]"
r"[0-9a-zA-Z-]{0,61})?[0-9a-zA-Z]\.[0-9a-zA-Z]{2,6})(:[0-9a-zA-Z]{1,4})?"
r"((\/[0-9a-zA-Z_!~*().;?:@&=+$,%#-]*)+)?")
result = url_regular.search(share_content)
if result:
# 重复判断一次,防止提取错误
if result.group(0) in share_content:
return result.group(0).strip()
else:
raise ValueError('请输入标准的抖音分享信息')
else:
raise ValueError('请输入标准的抖音分享信息')


def resolve_share_url(share_url: str) -> str:
"""
解析分享链接,提取作品ID
:param share_url:
:return:
"""
resp = requests.get(share_url, verify=False)
# url解码
url_data = parse.unquote(resp.url)
# url结果
result = parse.urlparse(url_data)
# url里的查询参数
video_id = str(result.path).split('/')[-2]
if video_id.isdigit():
return video_id
else:
raise ValueError('获取作品ID异常,请检查连接:%s是否为抖音分享链接。' % share_url)


def get_tiktok_item_info(share_content: str) -> dict:
share_url = handle_share_content(share_content)
item_id = resolve_share_url(share_url)

response = requests.get("https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/",
params={'item_ids': item_id}, verify=False, timeout=5).json()

if response['status_code'] == 0 and len(response['item_list']) > 0:
return response['item_list'][0]
else:
# 数据不存在
raise RuntimeError('获取抖音Api数据失败,%s' % response)

data = '尝试臭豆腐肥肠火锅?这味道简直太太太上头了!!%vlog日常 %美食探店 %火锅 %深夜放毒 %美食趣胃计划' \
'https://v.douyin.com/eNBofpr/ 椱制此链接,打开Dou吟搜索,直接觀看视频!'

info = get_tiktok_item_info(share_content=data)
print(info)

### OutPut
{'is_live_replay': False, 'video_labels': None, 'geofencing': None, 'group_id': 6942073391922843000, 'forward_id': '0', 'author': {'policy_version': None, 'uid': '76615500441', 'short_id': '2262937117', 'avatar_thumb': {'uri': '100x100/20b7b00015cd04a8ec0cb', 'url_list': ['https://p5-ipv6.douyinpic.com/aweme/100x100/20b7b00015cd04a8ec0cb.jpeg?from=2563711402', 'https://p6.douyinpic.com/aweme/100x100/20b7b00015cd04a8ec0cb.jpeg?from=2563711402', 'https://p11.douyinpic.com/aweme/100x100/20b7b00015cd04a8ec0cb.jpeg?from=2563711402']}, 'avatar_medium': {'uri': '720x720/20b7b00015cd04a8ec0cb', 'url_list': ['https://p3.douyinpic.com/aweme/720x720/20b7b00015cd04a8ec0cb.jpeg?from=2563711402', 'https://p29.douyinpic.com/aweme/720x720/20b7b00015cd04a8ec0cb.jpeg?from=2563711402', 'https://p26.douyinpic.com/aweme/720x720/20b7b00015cd04a8ec0cb.jpeg?from=2563711402']}, 'unique_id': 'xiaozhuzhu2334', 'platform_sync_info': None, 'nickname': '帅气的悉尼小猪猪', 'signature': '🧣同名\n合作:taozi484520(只接星图)', 'avatar_larger': {'uri': '1080x1080/20b7b00015cd04a8ec0cb', 'url_list': ['https://p3.douyinpic.com/aweme/1080x1080/20b7b00015cd04a8ec0cb.jpeg?from=2563711402', 'https://p5-ipv6.douyinpic.com/aweme/1080x1080/20b7b00015cd04a8ec0cb.jpeg?from=2563711402', 'https://p26.douyinpic.com/aweme/1080x1080/20b7b00015cd04a8ec0cb.jpeg?from=2563711402']}, 'followers_detail': None, 'geofencing': None, 'type_label': None}, 'video': {'origin_cover': {'uri': 'tos-cn-p-0015/8111a1cf615e48d8a06d8363c91e8250_1616327449', 'url_list': ['https://p29.douyinpic.com/tos-cn-p-0015/8111a1cf615e48d8a06d8363c91e8250_1616327449~tplv-dy-360p.jpeg?from=2563711402', 'https://p6.douyinpic.com/tos-cn-p-0015/8111a1cf615e48d8a06d8363c91e8250_1616327449~tplv-dy-360p.jpeg?from=2563711402', 'https://p5-ipv6.douyinpic.com/tos-cn-p-0015/8111a1cf615e48d8a06d8363c91e8250_1616327449~tplv-dy-360p.jpeg?from=2563711402']}, 'ratio': '720p', 'duration': 90139, 'is_long_video': 1, 'play_addr': {'uri': 'v0200fcf0000c1bj4fmluf3igni150tg', 'url_list': ['https://aweme.snssdk.com/aweme/v1/playwm/?video_id=v0200fcf0000c1bj4fmluf3igni150tg&ratio=720p&line=0']}, 'cover': {'url_list': ['https://p29.douyinpic.com/img/tos-cn-p-0015/fdabcc862cbc4698a4ee4feb42a9ee27~c5_300x400.jpeg?from=2563711402_large', 'https://p26.douyinpic.com/img/tos-cn-p-0015/fdabcc862cbc4698a4ee4feb42a9ee27~c5_300x400.jpeg?from=2563711402_large', 'https://p5-ipv6.douyinpic.com/img/tos-cn-p-0015/fdabcc862cbc4698a4ee4feb42a9ee27~c5_300x400.jpeg?from=2563711402_large'], 'uri': 'tos-cn-p-0015/fdabcc862cbc4698a4ee4feb42a9ee27'}, 'dynamic_cover': {'uri': 'tos-cn-p-0015/fdabcc862cbc4698a4ee4feb42a9ee27', 'url_list': ['https://p5-ipv6.douyinpic.com/obj/tos-cn-p-0015/fdabcc862cbc4698a4ee4feb42a9ee27?from=2563711402_large', 'https://p26.douyinpic.com/obj/tos-cn-p-0015/fdabcc862cbc4698a4ee4feb42a9ee27?from=2563711402_large', 'https://p3.douyinpic.com/obj/tos-cn-p-0015/fdabcc862cbc4698a4ee4feb42a9ee27?from=2563711402_large']}, 'bit_rate': None, 'vid': 'v0200fcf0000c1bj4fmluf3igni150tg', 'height': 1920, 'width': 1080, 'has_watermark': True}, 'share_info': {'share_weibo_desc': '#在抖音,记录美好生活#尝试臭豆腐肥肠火锅?这味道简直太太太上头了!!#vlog日常 #美食探店 #火锅 #深夜放毒 #美食趣胃计划', 'share_desc': '在抖音,记录美好生活', 'share_title': '尝试臭豆腐肥肠火锅?这味道简直太太太上头了!!#vlog日常 #美食探店 #火锅 #深夜放毒 #美食趣胃计划'}, 'video_text': None, 'create_time': 1616328562, 'cha_list': [{'user_count': 0, 'is_commerce': False, 'cid': '1558567968378882', 'cha_name': '深夜放毒', 'desc': '', 'hash_tag_profile': '', 'connect_music': None, 'type': 0, 'view_count': 0}], 'image_infos': None, 'author_user_id': 76615500441, 'desc': '尝试臭豆腐肥肠火锅?这味道简直太太太上头了!!#vlog日常 #美食探店 #火锅 #深夜放毒 #美食趣胃计划', 'images': None, 'promotions': None, 'share_url': 'https://www.iesdouyin.com/share/video/6942073391922842894/?region=&mid=6942073520700607269&u_code=48&titleType=title&did=MS4wLjABAAAANwkJuWIRFOzg5uCpDRpMj4OX-QryoDgn-yYlXQnRwQQ&iid=MS4wLjABAAAANwkJuWIRFOzg5uCpDRpMj4OX-QryoDgn-yYlXQnRwQQ&with_sec_did=1', 'label_top_text': None, 'mix_info': {'mix_id': '6812903915307141134', 'mix_name': '猪猪的美食vlog!', 'statis': {'updated_to_episode': 55, 'play_vv': 0, 'collect_vv': 0, 'current_episode': 55}, 'extra': '{"douyin_search_ocr_content":"猪猪带你吃吃吃,吃饱了才能减肥嘛!","douyin_search_user_generated_title":" 美食 计划 ","first_reviewed":1,"is_quality_mix":0,"next_info":{"cover":"tos-cn-i-0813/2cebe1ba64a9413d8e85383a0651a64d","desc":"猪猪带你吃吃吃,吃饱了才能减肥嘛!","name":"猪猪的美食vlog!"},"origin_challenge_content":""}', 'create_time': 1586252804, 'cover_url': {'url_list': ['https://p5-ipv6.douyinpic.com/obj/tos-cn-i-0813/2cebe1ba64a9413d8e85383a0651a64d?from=2563711402', 'https://p26.douyinpic.com/obj/tos-cn-i-0813/2cebe1ba64a9413d8e85383a0651a64d?from=2563711402', 'https://p11.douyinpic.com/obj/tos-cn-i-0813/2cebe1ba64a9413d8e85383a0651a64d?from=2563711402'], 'uri': 'tos-cn-i-0813/2cebe1ba64a9413d8e85383a0651a64d'}, 'status': {'status': 2, 'is_collected': 0}, 'desc': '猪猪带你吃吃吃,吃饱了才能减肥嘛!', 'next_info': {'mix_name': '猪猪的美食vlog!', 'desc': '猪猪带你吃吃吃,吃饱了才能减肥嘛!', 'cover_url': {'url_list': ['https://p1-dy.bytecdn.cn/obj/tos-cn-i-0813/2cebe1ba64a9413d8e85383a0651a64d', 'https://p3-dy.bytecdn.cn/obj/tos-cn-i-0813/2cebe1ba64a9413d8e85383a0651a64d', 'https://p9-dy.bytecdn.cn/obj/tos-cn-i-0813/2cebe1ba64a9413d8e85383a0651a64d'], 'uri': 'tos-cn-i-0813/2cebe1ba64a9413d8e85383a0651a64d'}}}, 'category': 0, 'is_preview': 1, 'aweme_id': '6942073391922842894', 'text_extra': [{'start': 23, 'end': 30, 'type': 1, 'hashtag_name': 'vlog日常', 'hashtag_id': 1607783788304387}, {'hashtag_name': '美食探店', 'hashtag_id': 1591486437085198, 'start': 31, 'end': 36, 'type': 1}, {'hashtag_name': '火锅', 'hashtag_id': 1553400994340865, 'start': 37, 'end': 40, 'type': 1}, {'start': 41, 'end': 46, 'type': 1, 'hashtag_name': '深夜放毒', 'hashtag_id': 1558567968378882}, {'hashtag_name': '美食趣胃计划', 'hashtag_id': 1657693004378126, 'start': 47, 'end': 54, 'type': 1}], 'aweme_type': 4, 'long_video': None, 'music': {'cover_medium': {'uri': '720x720/20b7b00015cd04a8ec0cb', 'url_list': ['https://p9.douyinpic.com/aweme/720x720/20b7b00015cd04a8ec0cb.jpeg?from=2563711402', 'https://p26.douyinpic.com/aweme/720x720/20b7b00015cd04a8ec0cb.jpeg?from=2563711402', 'https://p11.douyinpic.com/aweme/720x720/20b7b00015cd04a8ec0cb.jpeg?from=2563711402']}, 'cover_thumb': {'uri': '168x168/20b7b00015cd04a8ec0cb', 'url_list': ['https://p5-ipv6.douyinpic.com/img/20b7b00015cd04a8ec0cb~c5_168x168.jpeg?from=2563711402', 'https://p11.douyinpic.com/img/20b7b00015cd04a8ec0cb~c5_168x168.jpeg?from=2563711402', 'https://p9.douyinpic.com/img/20b7b00015cd04a8ec0cb~c5_168x168.jpeg?from=2563711402']}, 'play_url': {'uri': 'https://sf6-cdn-tos.douyinstatic.com/obj/ies-music/6942073506247379719.mp3', 'url_list': ['https://sf6-cdn-tos.douyinstatic.com/obj/ies-music/6942073506247379719.mp3', 'https://sf3-cdn-tos.douyinstatic.com/obj/ies-music/6942073506247379719.mp3']}, 'duration': 90, 'position': None, 'mid': '6942073520700607269', 'title': '@帅气的悉尼小猪猪创作的原声', 'cover_hd': {'uri': '1080x1080/20b7b00015cd04a8ec0cb', 'url_list': ['https://p26.douyinpic.com/aweme/1080x1080/20b7b00015cd04a8ec0cb.jpeg?from=2563711402', 'https://p29.douyinpic.com/aweme/1080x1080/20b7b00015cd04a8ec0cb.jpeg?from=2563711402', 'https://p11.douyinpic.com/aweme/1080x1080/20b7b00015cd04a8ec0cb.jpeg?from=2563711402']}, 'cover_large': {'uri': '1080x1080/20b7b00015cd04a8ec0cb', 'url_list': ['https://p26.douyinpic.com/aweme/1080x1080/20b7b00015cd04a8ec0cb.jpeg?from=2563711402', 'https://p29.douyinpic.com/aweme/1080x1080/20b7b00015cd04a8ec0cb.jpeg?from=2563711402', 'https://p11.douyinpic.com/aweme/1080x1080/20b7b00015cd04a8ec0cb.jpeg?from=2563711402']}, 'status': 1, 'id': 6942073520700607000, 'author': '帅气的悉尼小猪猪'}, 'duration': 90139, 'risk_infos': {'warn': False, 'type': 0, 'content': ''}, 'comment_list': None, 'anchor_info': {'id': '6867104903072860167', 'type': 2}, 'statistics': {'aweme_id': '6942073391922842894', 'comment_count': 15234, 'digg_count': 149138, 'play_count': 0, 'share_count': 4860}}

抖音无水印视频

接口分析

上一小结获取的抖音视频信息中,可以在返回结果的video -> play_addr -> url_list找到视频的地址,但是是有水印的。有没有办法获取无水印的视频呢?

观察视频地址:https://aweme.snssdk.com/aweme/v1/playwm/?video_id=v0200fcf0000c1bj4fmluf3igni150tg&ratio=720p&line=0

尝试去掉参数之后发现仍可以正常访问,所以和传入的参数ratioline关系不大。正当一筹莫展之际,突然发现https://aweme.snssdk.com/aweme/v1/playwm/ 中的最后一段地址有点奇怪,playvm不像是一个单词。猜想wm是英文水印(water mark)的缩写。

果然,去掉之后是无水印的版本

代码

1
2
3
4
5
6
7
8
9
# info 为上一节返回的作品信息,这里直接定义,不在重复
info = {'is_live_replay': False, 'video_labels': None, 'geofencing': None, 'group_id': 6942073391922843000, 'forward_id': '0', 'author': {'policy_version': None, 'uid': '76615500441', 'short_id': '2262937117', 'avatar_thumb': {'uri': '100x100/20b7b00015cd04a8ec0cb', 'url_list': ['https://p5-ipv6.douyinpic.com/aweme/100x100/20b7b00015cd04a8ec0cb.jpeg?from=2563711402', 'https://p6.douyinpic.com/aweme/100x100/20b7b00015cd04a8ec0cb.jpeg?from=2563711402', 'https://p11.douyinpic.com/aweme/100x100/20b7b00015cd04a8ec0cb.jpeg?from=2563711402']}, 'avatar_medium': {'uri': '720x720/20b7b00015cd04a8ec0cb', 'url_list': ['https://p3.douyinpic.com/aweme/720x720/20b7b00015cd04a8ec0cb.jpeg?from=2563711402', 'https://p29.douyinpic.com/aweme/720x720/20b7b00015cd04a8ec0cb.jpeg?from=2563711402', 'https://p26.douyinpic.com/aweme/720x720/20b7b00015cd04a8ec0cb.jpeg?from=2563711402']}, 'unique_id': 'xiaozhuzhu2334', 'platform_sync_info': None, 'nickname': '帅气的悉尼小猪猪', 'signature': '🧣同名\n合作:taozi484520(只接星图)', 'avatar_larger': {'uri': '1080x1080/20b7b00015cd04a8ec0cb', 'url_list': ['https://p3.douyinpic.com/aweme/1080x1080/20b7b00015cd04a8ec0cb.jpeg?from=2563711402', 'https://p5-ipv6.douyinpic.com/aweme/1080x1080/20b7b00015cd04a8ec0cb.jpeg?from=2563711402', 'https://p26.douyinpic.com/aweme/1080x1080/20b7b00015cd04a8ec0cb.jpeg?from=2563711402']}, 'followers_detail': None, 'geofencing': None, 'type_label': None}, 'video': {'origin_cover': {'uri': 'tos-cn-p-0015/8111a1cf615e48d8a06d8363c91e8250_1616327449', 'url_list': ['https://p29.douyinpic.com/tos-cn-p-0015/8111a1cf615e48d8a06d8363c91e8250_1616327449~tplv-dy-360p.jpeg?from=2563711402', 'https://p6.douyinpic.com/tos-cn-p-0015/8111a1cf615e48d8a06d8363c91e8250_1616327449~tplv-dy-360p.jpeg?from=2563711402', 'https://p5-ipv6.douyinpic.com/tos-cn-p-0015/8111a1cf615e48d8a06d8363c91e8250_1616327449~tplv-dy-360p.jpeg?from=2563711402']}, 'ratio': '720p', 'duration': 90139, 'is_long_video': 1, 'play_addr': {'uri': 'v0200fcf0000c1bj4fmluf3igni150tg', 'url_list': ['https://aweme.snssdk.com/aweme/v1/playwm/?video_id=v0200fcf0000c1bj4fmluf3igni150tg&ratio=720p&line=0']}, 'cover': {'url_list': ['https://p29.douyinpic.com/img/tos-cn-p-0015/fdabcc862cbc4698a4ee4feb42a9ee27~c5_300x400.jpeg?from=2563711402_large', 'https://p26.douyinpic.com/img/tos-cn-p-0015/fdabcc862cbc4698a4ee4feb42a9ee27~c5_300x400.jpeg?from=2563711402_large', 'https://p5-ipv6.douyinpic.com/img/tos-cn-p-0015/fdabcc862cbc4698a4ee4feb42a9ee27~c5_300x400.jpeg?from=2563711402_large'], 'uri': 'tos-cn-p-0015/fdabcc862cbc4698a4ee4feb42a9ee27'}, 'dynamic_cover': {'uri': 'tos-cn-p-0015/fdabcc862cbc4698a4ee4feb42a9ee27', 'url_list': ['https://p5-ipv6.douyinpic.com/obj/tos-cn-p-0015/fdabcc862cbc4698a4ee4feb42a9ee27?from=2563711402_large', 'https://p26.douyinpic.com/obj/tos-cn-p-0015/fdabcc862cbc4698a4ee4feb42a9ee27?from=2563711402_large', 'https://p3.douyinpic.com/obj/tos-cn-p-0015/fdabcc862cbc4698a4ee4feb42a9ee27?from=2563711402_large']}, 'bit_rate': None, 'vid': 'v0200fcf0000c1bj4fmluf3igni150tg', 'height': 1920, 'width': 1080, 'has_watermark': True}, 'share_info': {'share_weibo_desc': '#在抖音,记录美好生活#尝试臭豆腐肥肠火锅?这味道简直太太太上头了!!#vlog日常 #美食探店 #火锅 #深夜放毒 #美食趣胃计划', 'share_desc': '在抖音,记录美好生活', 'share_title': '尝试臭豆腐肥肠火锅?这味道简直太太太上头了!!#vlog日常 #美食探店 #火锅 #深夜放毒 #美食趣胃计划'}, 'video_text': None, 'create_time': 1616328562, 'cha_list': [{'user_count': 0, 'is_commerce': False, 'cid': '1558567968378882', 'cha_name': '深夜放毒', 'desc': '', 'hash_tag_profile': '', 'connect_music': None, 'type': 0, 'view_count': 0}], 'image_infos': None, 'author_user_id': 76615500441, 'desc': '尝试臭豆腐肥肠火锅?这味道简直太太太上头了!!#vlog日常 #美食探店 #火锅 #深夜放毒 #美食趣胃计划', 'images': None, 'promotions': None, 'share_url': 'https://www.iesdouyin.com/share/video/6942073391922842894/?region=&mid=6942073520700607269&u_code=48&titleType=title&did=MS4wLjABAAAANwkJuWIRFOzg5uCpDRpMj4OX-QryoDgn-yYlXQnRwQQ&iid=MS4wLjABAAAANwkJuWIRFOzg5uCpDRpMj4OX-QryoDgn-yYlXQnRwQQ&with_sec_did=1', 'label_top_text': None, 'mix_info': {'mix_id': '6812903915307141134', 'mix_name': '猪猪的美食vlog!', 'statis': {'updated_to_episode': 55, 'play_vv': 0, 'collect_vv': 0, 'current_episode': 55}, 'extra': '{"douyin_search_ocr_content":"猪猪带你吃吃吃,吃饱了才能减肥嘛!","douyin_search_user_generated_title":" 美食 计划 ","first_reviewed":1,"is_quality_mix":0,"next_info":{"cover":"tos-cn-i-0813/2cebe1ba64a9413d8e85383a0651a64d","desc":"猪猪带你吃吃吃,吃饱了才能减肥嘛!","name":"猪猪的美食vlog!"},"origin_challenge_content":""}', 'create_time': 1586252804, 'cover_url': {'url_list': ['https://p5-ipv6.douyinpic.com/obj/tos-cn-i-0813/2cebe1ba64a9413d8e85383a0651a64d?from=2563711402', 'https://p26.douyinpic.com/obj/tos-cn-i-0813/2cebe1ba64a9413d8e85383a0651a64d?from=2563711402', 'https://p11.douyinpic.com/obj/tos-cn-i-0813/2cebe1ba64a9413d8e85383a0651a64d?from=2563711402'], 'uri': 'tos-cn-i-0813/2cebe1ba64a9413d8e85383a0651a64d'}, 'status': {'status': 2, 'is_collected': 0}, 'desc': '猪猪带你吃吃吃,吃饱了才能减肥嘛!', 'next_info': {'mix_name': '猪猪的美食vlog!', 'desc': '猪猪带你吃吃吃,吃饱了才能减肥嘛!', 'cover_url': {'url_list': ['https://p1-dy.bytecdn.cn/obj/tos-cn-i-0813/2cebe1ba64a9413d8e85383a0651a64d', 'https://p3-dy.bytecdn.cn/obj/tos-cn-i-0813/2cebe1ba64a9413d8e85383a0651a64d', 'https://p9-dy.bytecdn.cn/obj/tos-cn-i-0813/2cebe1ba64a9413d8e85383a0651a64d'], 'uri': 'tos-cn-i-0813/2cebe1ba64a9413d8e85383a0651a64d'}}}, 'category': 0, 'is_preview': 1, 'aweme_id': '6942073391922842894', 'text_extra': [{'start': 23, 'end': 30, 'type': 1, 'hashtag_name': 'vlog日常', 'hashtag_id': 1607783788304387}, {'hashtag_name': '美食探店', 'hashtag_id': 1591486437085198, 'start': 31, 'end': 36, 'type': 1}, {'hashtag_name': '火锅', 'hashtag_id': 1553400994340865, 'start': 37, 'end': 40, 'type': 1}, {'start': 41, 'end': 46, 'type': 1, 'hashtag_name': '深夜放毒', 'hashtag_id': 1558567968378882}, {'hashtag_name': '美食趣胃计划', 'hashtag_id': 1657693004378126, 'start': 47, 'end': 54, 'type': 1}], 'aweme_type': 4, 'long_video': None, 'music': {'cover_medium': {'uri': '720x720/20b7b00015cd04a8ec0cb', 'url_list': ['https://p9.douyinpic.com/aweme/720x720/20b7b00015cd04a8ec0cb.jpeg?from=2563711402', 'https://p26.douyinpic.com/aweme/720x720/20b7b00015cd04a8ec0cb.jpeg?from=2563711402', 'https://p11.douyinpic.com/aweme/720x720/20b7b00015cd04a8ec0cb.jpeg?from=2563711402']}, 'cover_thumb': {'uri': '168x168/20b7b00015cd04a8ec0cb', 'url_list': ['https://p5-ipv6.douyinpic.com/img/20b7b00015cd04a8ec0cb~c5_168x168.jpeg?from=2563711402', 'https://p11.douyinpic.com/img/20b7b00015cd04a8ec0cb~c5_168x168.jpeg?from=2563711402', 'https://p9.douyinpic.com/img/20b7b00015cd04a8ec0cb~c5_168x168.jpeg?from=2563711402']}, 'play_url': {'uri': 'https://sf6-cdn-tos.douyinstatic.com/obj/ies-music/6942073506247379719.mp3', 'url_list': ['https://sf6-cdn-tos.douyinstatic.com/obj/ies-music/6942073506247379719.mp3', 'https://sf3-cdn-tos.douyinstatic.com/obj/ies-music/6942073506247379719.mp3']}, 'duration': 90, 'position': None, 'mid': '6942073520700607269', 'title': '@帅气的悉尼小猪猪创作的原声', 'cover_hd': {'uri': '1080x1080/20b7b00015cd04a8ec0cb', 'url_list': ['https://p26.douyinpic.com/aweme/1080x1080/20b7b00015cd04a8ec0cb.jpeg?from=2563711402', 'https://p29.douyinpic.com/aweme/1080x1080/20b7b00015cd04a8ec0cb.jpeg?from=2563711402', 'https://p11.douyinpic.com/aweme/1080x1080/20b7b00015cd04a8ec0cb.jpeg?from=2563711402']}, 'cover_large': {'uri': '1080x1080/20b7b00015cd04a8ec0cb', 'url_list': ['https://p26.douyinpic.com/aweme/1080x1080/20b7b00015cd04a8ec0cb.jpeg?from=2563711402', 'https://p29.douyinpic.com/aweme/1080x1080/20b7b00015cd04a8ec0cb.jpeg?from=2563711402', 'https://p11.douyinpic.com/aweme/1080x1080/20b7b00015cd04a8ec0cb.jpeg?from=2563711402']}, 'status': 1, 'id': 6942073520700607000, 'author': '帅气的悉尼小猪猪'}, 'duration': 90139, 'risk_infos': {'warn': False, 'type': 0, 'content': ''}, 'comment_list': None, 'anchor_info': {'id': '6867104903072860167', 'type': 2}, 'statistics': {'aweme_id': '6942073391922842894', 'comment_count': 15234, 'digg_count': 149138, 'play_count': 0, 'share_count': 4860}}


def wipe_watermark(item_info: dict) -> str:
video_url = item_info['video']['play_addr']['url_list'][0]
return str(video_url).replace("/v1/playwm/?", "/v1/play/?")

print(wipe_watermark(item_info=info))

抖音作者信息

接口分析

作者信息和作品信息类似,通过客户端拿到分享链接,打开调试模式获取真实接口地址

1
2
3
分享链接:https://v.douyin.com/eNAud7S/
重定向链接:https://www.iesdouyin.com/share/user/76615500441?u_code=19i75j4k2a8c&did=2471341373012760&iid=1407810874127151&sec_uid=MS4wLjABAAAAdeyz1L3UsIm98XsqJyB8DGRgqG_lRWApIECVDSnFQYE&app=aweme&utm_campaign=client_share&utm_medium=ios&tt_from=copy&utm_source=copy
数据接口地址:https://www.iesdouyin.com/web/api/v2/user/info/?sec_uid=MS4wLjABAAAAdeyz1L3UsIm98XsqJyB8DGRgqG_lRWApIECVDSnFQYE

经测试,重定向后的地址参数只有sec_uid是有用的,甚至url结尾的用户ID都是没用的,例如以下两个链接返回的都是相同的信息

1
2
https://www.iesdouyin.com/share/user/76615500441?sec_uid=MS4wLjABAAAAdeyz1L3UsIm98XsqJyB8DGRgqG_lRWApIECVDSnFQYE
https://www.iesdouyin.com/share/user/我是帅哥?sec_uid=MS4wLjABAAAAdeyz1L3UsIm98XsqJyB8DGRgqG_lRWApIECVDSnFQYE

虽然能理解接口逻辑,但是这种操作还是蛮奇怪的。那为什么不干脆直接去掉后面的ID呢。😳

信息收集的差不多了,整理一下:

  1. 获取分享链接
  2. 访问分享链接
  3. 通过重定向后的分享链接获取用户的sec_uid
  4. 调用API接口,获取作者信息

逻辑和获取作品信息的非常非常像了,只不过用户无法直接通过id获取数据,猜想是因为ID是数字,容易被爬虫递增爬取。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import re
import requests

from urllib import parse

# 关闭烦人提示
requests.packages.urllib3.disable_warnings()

def handle_share_content(share_content: str) -> str:
"""
从App端分享信息中提取分享短链接
:param share_content:
:return:
"""
url_regular = re.compile(r"(https|http)?:\/\/(([0-9]{1,3}\.){3}[0-9]{1,3}|([0-9a-zA-Z_!~*'()-]+\.)*([0-9a-zA-Z]"
r"[0-9a-zA-Z-]{0,61})?[0-9a-zA-Z]\.[0-9a-zA-Z]{2,6})(:[0-9a-zA-Z]{1,4})?"
r"((\/[0-9a-zA-Z_!~*().;?:@&=+$,%#-]*)+)?")
result = url_regular.search(share_content)
if result:
# 重复判断一次,防止提取错误
if result.group(0) in share_content:
return result.group(0).strip()
else:
raise ValueError('请输入标准的抖音分享信息')
else:
raise ValueError('请输入标准的抖音分享信息')


def resolve_share_url(share_url: str) -> str:
"""
解析分享链接,提取用户sec_uid
:param share_url:
:return:
"""
resp = requests.get(share_url, verify=False)
# url解码
url_data = parse.unquote(resp.url)
# url结果
result = parse.urlparse(url_data)
# url里的查询参数
query_dict = parse.parse_qs(result.query)
sec_uid = query_dict.get('sec_uid')
if sec_uid:
return sec_uid[0]
else:
raise ValueError('获取作者ID异常,请检查连接:%s是否为抖音分享链接。' % share_url)


def get_tiktok_user_info(share_content: str) -> dict:
share_url = handle_share_content(share_content)
sec_uid = resolve_share_url(share_url)
response = requests.get("https://www.iesdouyin.com/web/api/v2/user/info/",
params={'sec_uid': sec_uid}, verify=False, timeout=5).json()

if response['status_code'] == 0 and 'user_info' in response:
return response['user_info']
else:
# 数据不存在
raise RuntimeError('获取抖音Api数据失败,%s' % response)

data = 'https://v.douyin.com/eNAud7S/'

info = get_tiktok_user_info(share_content=data)
print(info)

### OutPut
{'follower_count': 6027000, 'unique_id': 'xiaozhuzhu2334', 'platform_sync_info': None, 'geofencing': None, 'signature': '🧣同名\n合作:taozi484520(只接星图)', 'aweme_count': 210, 'nickname': '帅气的悉尼小猪猪', 'avatar_medium': {'uri': '720x720/20b7b00015cd04a8ec0cb', 'url_list': ['https://p11.douyinpic.com/aweme/720x720/20b7b00015cd04a8ec0cb.jpeg?from=2956013662', 'https://p5-ipv6.douyinpic.com/aweme/720x720/20b7b00015cd04a8ec0cb.jpeg?from=2956013662', 'https://p3.douyinpic.com/aweme/720x720/20b7b00015cd04a8ec0cb.jpeg?from=2956013662']}, 'verification_type': 1, 'region': 'CN', 'uid': '76615500441', 'short_id': '2262937117', 'following_count': 2, 'favoriting_count': 481, 'total_favorited': '92187000', 'original_musician': {'music_count': 0, 'music_used_count': 0}, 'secret': 0, 'is_gov_media_vip': False, 'avatar_larger': {'uri': '1080x1080/20b7b00015cd04a8ec0cb', 'url_list': ['https://p29.douyinpic.com/aweme/1080x1080/20b7b00015cd04a8ec0cb.jpeg?from=2956013662', 'https://p5-ipv6.douyinpic.com/aweme/1080x1080/20b7b00015cd04a8ec0cb.jpeg?from=2956013662', 'https://p9.douyinpic.com/aweme/1080x1080/20b7b00015cd04a8ec0cb.jpeg?from=2956013662']}, 'avatar_thumb': {'url_list': ['https://p29.douyinpic.com/aweme/100x100/20b7b00015cd04a8ec0cb.jpeg?from=2956013662', 'https://p26.douyinpic.com/aweme/100x100/20b7b00015cd04a8ec0cb.jpeg?from=2956013662', 'https://p5-ipv6.douyinpic.com/aweme/100x100/20b7b00015cd04a8ec0cb.jpeg?from=2956013662'], 'uri': '100x100/20b7b00015cd04a8ec0cb'}, 'policy_version': None, 'type_label': None, 'custom_verify': '', 'followers_detail': None}

总结

总体看来,抖音的数据还是非常容易爬取的,接口逻辑非常清晰简单。有机会的话后面聊一聊小红书,小红书的反爬做的是非常厉害。