命令行批量下载抖音单用户视频【增强版】
第一次发帖如果有问题还请版主大大见谅。首先感谢Quincy_007大神:https://www.52pojie.cn/forum.php?mod=viewthread&tid=1431173给出的底座,我在此基础上增加了如下功能:
更新内容:1. 增加了按照下载顺序以序号命名文件名,方便索引
2. 增加了通过参数传递URL的方式下载
3. 对URL输入错误给出了友好的提示和程序退出方式
4. 增强了对文件名特殊符号的转义功能
5. 增加了下载时间统计的功能
6. 增加了下载结束后调用系统发声的功能,方便做其他事情的时候有个提示
7. 操作文字傻瓜化,界面美观化
优势在于可以用DOS的FOR命令把要下载的用户短链接都放在1.txt中,然后用
for /f %%i in (1.txt) do (python dydl.py %%i)
批量下载所有想下载用户的全部视频
TODO:
1. 经过实践发现下载的数量和抖音官方统计出来的数量不一致,不知道为什么
2. 下载多了有时候会卡进程,想增加一个判断超时重下当前视频的功能,还没有思路
源代码如下:
import requestsimport json
import os
import time
import re
import sys
import winsound
"""
1.根据用户页面分享的字符串提取短url
2.根据短url加上302获取location,提取sec_id
3.拼接视频列表请求url
params = {
'sec_uid' : 'MS4wLjABAAAAbtSlJK_BfUcuqyy8ypNouqEH7outUXePTYEcAIpY9rk',
'count' : '200',
'min_cursor' : '1612108800000',
'max_cursor' : '1619251716404',
'aid' : '1128',
'_signature' : 'PtCNCgAAXljWCq93QOKsFT7QjR'
}
"""
headers = {
"user-agent": "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Mobile Safari/537.36"
}
##############################################
# 可以选择:
# 1文件内编辑好
# 2交互窗口输入
# 3命令行传参
# string= 'https://v.douyin.com/ePPVX3Q/'
# string = input('INPUT URL (like:【在抖音,记录美好生活! https://v.douyin.com/ekkTsYw/】):')
if(len(sys.argv)==1):
string = input('INPUT URL (like:【在抖音,记录美好生活! https://v.douyin.com/ekkTsYw/】):')
else:
string = sys.argv
#############################################
try:
shroturl = re.findall('+://[\S]+', string, re.I|re.M)
except IndexError:
print('链接读取错误,程序退出')
sys.exit(1)
else:
print('短链接:'+shroturl)
startpage = requests.get(url=shroturl, headers=headers, allow_redirects=False)
location = startpage.headers['location']
sec_uid = re.findall('(?<=sec_uid=)+', location, re.M|re.I)
getname = requests.get(url='https://www.iesdouyin.com/web/api/v2/user/info/?sec_uid={}'.format(sec_uid), headers=headers).text
userinfo = json.loads(getname)
name = userinfo['user_info']['nickname']
print('用户名:'+userinfo['user_info']['nickname'])
Path = name
if os.path.exists(path=Path) == False:
os.mkdir(path=Path)
else:
print('目录已存在')
os.chdir(path=Path)
"""new function"""
year = ('2018','2019','2020','2021')
month = ('01','02','03','04','05','06','07','08','09','10','11','12')
j=1
zx_time_old=time.time()
#timepool =
timepool =
# print(timepool)
k = len(timepool)
for i in range(k) :
if i < k-1 :
beginarray = time.strptime(timepool, "%Y-%m-%d")
#beginarray = time.strptime(timepool, "%Y-%m-%d %H:%M:%S")
endarray = time.strptime(timepool, "%Y-%m-%d")
#endarray = time.strptime(timepool, "%Y-%m-%d %H:%M:%S")
t1 = int(time.mktime(beginarray) * 1000)
t2 = int(time.mktime(endarray) * 1000)
# print(t1,t2)
params = {
'sec_uid' : sec_uid,
'count' : 200,
'min_cursor' : t1,
'max_cursor' : t2,
'aid' : 1128,
'_signature' : 'PtCNCgAAXljWCq93QOKsFT7QjR'
}
awemeurl = 'https://www.iesdouyin.com/web/api/v2/aweme/post/?'
awemehtml = requests.get(url=awemeurl, params=params, headers=headers).text
data = json.loads(awemehtml)
# print(data)
# print(type(data))
awemenum = len(data['aweme_list'])
if(awemenum > 0):
print('=============================================')
print('从'+timepool+'到'+timepool+'共有【'+str(awemenum)+'】个视频')
print('=============================================')
#print(awemenum)
for i in range(awemenum):
# 将文件名中含有Windows下有可能会产生错误的符号进行转义,变成全角符号
videotitle = data['aweme_list']['desc'].replace("?", "").replace("\"","\").replace("\x83","\").replace("\x08","\").replace("\n","\").replace("\r","\").replace(":",":").replace("/","/").replace("|","|").replace(":",":").replace("#","#").replace(">",">").replace("<","<").replace("@抖音小助手","").replace("@DOU+小助手","").replace("@抖音科技","").replace("@抖音财经","").replace("@DOU知计划","")
# 将生成的文件名从0开始排序,序号保留四位
videotitle = str(j).rjust(4,'0')+'.'+videotitle
# 全局计数从旧到新命名下载的文件
j=j+1
if (j>-1):
#print ('videotitle:'+videotitle)
videourl = data['aweme_list']['video']['play_addr']['url_list']
#print ('videourl:'+videourl)
start = time.time()
# print('视频:{} 开始下载'.format(videotitle))
with open(videotitle+'.mp4', 'wb') as v:
try:
v.write(requests.get(url=videourl, headers=headers).content)
end = time.time()
cost = end - start
print('{}下载耗时: {}秒'.format(videotitle, round(cost,2)))
except Exception as e:
print('下载错误')
zx_time_new=time.time()
duration=zx_time_new-zx_time_old
print('总计下载'+str(j)+'个文件,共耗时'+str(round(duration,2))+'秒,平均每个视频下载用时'+str(round(duration/j,2))+'秒')
winsound.Beep(1000,700)
import requests
import json
import os
import time
import re
import sys
"""
1.根据用户页面分享的字符串提取短url
2.根据短url加上302获取location,提取sec_id
3.拼接视频列表请求url
params = {
'sec_uid' : 'MS4wLjABAAAAbtSlJK_BfUcuqyy8ypNouqEH7outUXePTYEcAIpY9rk',
'count' : '200',
'min_cursor' : '1612108800000',
'max_cursor' : '1619251716404',
'aid' : '1128',
'_signature' : 'PtCNCgAAXljWCq93QOKsFT7QjR'
}
"""
headers = {
"user-agent": "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Mobile Safari/537.36"
}
##############################################
# 可以选择:
# 1文件内编辑好
# 2交互窗口输入
# 3命令行传参
# string= 'https://v.douyin.com/ePPVX3Q/'
# string = input('INPUT URL (like:【在抖音,记录美好生活! https://v.douyin.com/ekkTsYw/】):')
if(len(sys.argv)==1):
string = input('INPUT URL (like:【在抖音,记录美好生活! https://v.douyin.com/ekkTsYw/】):')
else:
string = sys.argv
#############################################
try:
shroturl = re.findall('+://[\S]+', string, re.I|re.M)
except IndexError:
print('链接读取错误,程序退出')
sys.exit(1)
else:
print('短链接:'+shroturl)
startpage = requests.get(url=shroturl, headers=headers, allow_redirects=False)
location = startpage.headers['location']
sec_uid = re.findall('(?<=sec_uid=)+', location, re.M|re.I)
getname = requests.get(url='https://www.iesdouyin.com/web/api/v2/user/info/?sec_uid={}'.format(sec_uid), headers=headers).text
userinfo = json.loads(getname)
name = userinfo['user_info']['nickname']
print('用户名:'+userinfo['user_info']['nickname'])
Path = name
if os.path.exists(path=Path) == False:
os.mkdir(path=Path)
else:
print('目录已存在')
os.chdir(path=Path)
"""new function"""
year = ('2018','2019','2020','2021')
month = ('01','02','03','04','05','06','07','08','09','10','11','12')
j=1
zx_time_old=time.time()
#timepool =
timepool =
# print(timepool)
k = len(timepool)
for i in range(k) :
if i < k-1 :
beginarray = time.strptime(timepool, "%Y-%m-%d")
#beginarray = time.strptime(timepool, "%Y-%m-%d %H:%M:%S")
endarray = time.strptime(timepool, "%Y-%m-%d")
#endarray = time.strptime(timepool, "%Y-%m-%d %H:%M:%S")
t1 = int(time.mktime(beginarray) * 1000)
t2 = int(time.mktime(endarray) * 1000)
# print(t1,t2)
params = {
'sec_uid' : sec_uid,
'count' : 200,
'min_cursor' : t1,
'max_cursor' : t2,
'aid' : 1128,
'_signature' : 'PtCNCgAAXljWCq93QOKsFT7QjR'
}
awemeurl = 'https://www.iesdouyin.com/web/api/v2/aweme/post/?'
awemehtml = requests.get(url=awemeurl, params=params, headers=headers).text
data = json.loads(awemehtml)
# print(data)
# print(type(data))
awemenum = len(data['aweme_list'])
if(awemenum > 0):
print('=============================================')
print('从'+timepool+'到'+timepool+'共有【'+str(awemenum)+'】个视频')
print('=============================================')
#print(awemenum)
for i in range(awemenum):
# 将文件名中含有Windows下有可能会产生错误的符号进行转义,变成全角符号
videotitle = data['aweme_list']['desc'].replace("?", "").replace("\"","\").replace("\x83","\").replace("\x08","\").replace("\n","\").replace("\r","\").replace(":",":").replace("/","/").replace("|","|").replace(":",":").replace("#","#").replace(">",">").replace("<","<").replace("@抖音小助手","").replace("@DOU+小助手","").replace("@抖音科技","").replace("@抖音财经","").replace("@DOU知计划","")
# 将生成的文件名从0开始排序,序号保留四位
videotitle = str(j).rjust(4,'0')+'.'+videotitle
# 全局计数从旧到新命名下载的文件
j=j+1
if (j>-1):
#print ('videotitle:'+videotitle)
videourl = data['aweme_list']['video']['play_addr']['url_list']
#print ('videourl:'+videourl)
start = time.time()
# print('视频:{} 开始下载'.format(videotitle))
with open(videotitle+'.mp4', 'wb') as v:
try:
v.write(requests.get(url=videourl, headers=headers).content)
end = time.time()
cost = end - start
print('{}下载耗时: {}秒'.format(videotitle, round(cost,2)))
except Exception as e:
print('下载错误')
zx_time_new=time.time()
duration=zx_time_new-zx_time_old
print('总计下载'+str(j)+'个文件,共耗时'+str(round(duration,2))+'秒,平均每个视频下载用时'+str(round(duration/j,2))+'秒')
原文中 有1处没有换行,然后 有个包 可有可无 还得下载,我给去除了。 zedex 发表于 2021-5-25 20:55
我遇到这个错误一般都是URL有问题
Traceback (most recent call last):
File "D:/学习/Python/pycharm/fg.py", line 210, in <module>
import requests
ModuleNotFoundError: No module named 'requests'
Process finished with exit code 1
请问这个是怎么回事? 谢谢分享。 非常不错,感谢楼主分享 感谢大佬分享 能不能實現自己輸入自己的號,
然後下載贊過的所有視頻?
然後下載所有關注的用戶的所有視頻? 非常不错 感谢感谢 很不错。厉害! 感谢分享 这款软件竟然可以下载喜欢的用户视频慢慢欣赏,比较给力!