|
#pid5271121{background-image:url("static/image/postbg/bg10.png");}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 info(self):
'''
获取item_ids和dytk参数
:return: [item_ids,dytk]
'''
url = self.url
response = requests.get(url, headers=headers)
now_url = response.url
pat_item_ids = '/video/(.*?)/'
self.item_ids = re.compile(pat_item_ids, re.S).findall(now_url)
pat_dytk = 'dytk: "(.*?)"'
self.dytk = re.compile(pat_dytk, re.S).findall(response.text)
print(self.item_ids, self.dytk)
return [self.item_ids[0], self.dytk[0]]
def video(self):
'''
获取无水印视频链接和视频描述
:return: true_url
'''
Douyin.info(self)
url = 'https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/'
params = {
'item_ids': self.item_ids,
'dytk': self.dytk
}
response = requests.get(url, 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']
print(self.true_url, self.des)
return [self.true_url, self.des]
def download(self):
'''
下载视频
:return:
'''
Douyin.video(self)
response = requests.get(self.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.write(response)
# 主程序
if __name__ == '__main__':
a = Douyin('https://v.douyin.com/38JgQ3/')
a.download()
复制代码
|
|