import requests
import re
import os
headers = {
'User-Agent': 'User-Agent", "Mozilla/5.0 iPhone; CPU iPhone OS 12_1_4 like Mac OS X) AppleWebKit/605.1.15 KHTML, like Gecko) Mobile/16D57 Version/12.0 Safari/604.1',
}
class Douyin:
def __init__self, url):
'''
数据初始化
:param url:
'''
self.url = url
# self.item_ids = ''
# self.dytk = ''
def infoself):
'''
获取item_ids和dytk参数
:return: [item_ids,dytk]
'''
url = self.url
response = requests.geturl, headers=headers)
now_url = response.url
pat_item_ids = '/video/.*?)/'
self.item_ids = re.compilepat_item_ids, re.S).findallnow_url)
pat_dytk = 'dytk: ".*?)"'
self.dytk = re.compilepat_dytk, re.S).findallresponse.text)
printself.item_ids, self.dytk)
return [self.item_ids[0], self.dytk[0]]
def videoself):
'''
获取无水印视频链接和视频描述
:return: true_url
'''
Douyin.infoself)
url = 'https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/'
params = {
'item_ids': self.item_ids,
'dytk': self.dytk
}
response = requests.geturl, headers=headers, params=params).json)
self.true_url = response['item_list'][0]['video']['play_addr']['url_list'][0]
self.des = response['item_list'][0]['desc']
printself.true_url, self.des)
return [self.true_url, self.des]
def downloadself):
'''
下载视频
:return:
'''
Douyin.videoself)
response = requests.getself.true_url, headers=headers).content
if os.path.exists'video') == False:
os.mkdir'video')
with open'video/' + self.des + '.mp4', mode='wb') as f:
f.writeresponse)
# 主程序
if __name__ == '__main__':
a = Douyin'https://v.douyin.com/38JgQ3/')
a.download)