吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 6594|回复: 39
收起左侧

[Python 转载] 命令行批量下载抖音单用户视频【增强版】

[复制链接]
zedex 发表于 2021-5-23 22:53
第一次发帖如果有问题还请版主大大见谅。

首先感谢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中,然后用
[Bash shell] 纯文本查看 复制代码
for /f %%i in (1.txt) do (python dydl.py %%i)

批量下载所有想下载用户的全部视频



TODO:
1. 经过实践发现下载的数量和抖音官方统计出来的数量不一致,不知道为什么
2. 下载多了有时候会卡进程,想增加一个判断超时重下当前视频的功能,还没有思路


源代码如下:

[Python] 纯文本查看 复制代码
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[1]
 #############################################
try:
    shroturl = re.findall('[a-z]+://[\S]+', string, re.I|re.M)[0]
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=)[a-z,A-Z,0-9, _, -]+', location, re.M|re.I)[0]
    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 = [x+'-'+y+'-01 00:00:00' for x in year for y in month ]
timepool = [x+'-'+y+'-01' for x in year for y in month ]
# print(timepool)
k = len(timepool)
for i in range(k) :
    if i < k-1 :
        beginarray = time.strptime(timepool[i], "%Y-%m-%d")
        #beginarray = time.strptime(timepool[i], "%Y-%m-%d %H:%M:%S")
        endarray = time.strptime(timepool[i+1], "%Y-%m-%d")
        #endarray = time.strptime(timepool[i+1], "%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[i]+'到'+timepool[i+1]+'共有【'+str(awemenum)+'】个视频')
           print('=============================================')
        #print(awemenum)
        for i in range(awemenum):
        # 将文件名中含有Windows下有可能会产生错误的符号进行转义,变成全角符号
            videotitle = data['aweme_list'][i]['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'][i]['video']['play_addr']['url_list'][0]
                #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)


免费评分

参与人数 5吾爱币 +4 热心值 +4 收起 理由
damide + 1 + 1 用心讨论,共获提升!
qq9953 + 1 谢谢@Thanks!
hackerchang + 1 + 1 我很赞同!
Βigbang + 1 请问有打包好的作品吗
水蜜桃好甜 + 1 + 1 我很赞同!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

ruicky 发表于 2021-5-25 10:10
[Python] 纯文本查看 复制代码
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[1]
 #############################################
try:
    shroturl = re.findall('[a-z]+://[\S]+', string, re.I|re.M)[0]
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=)[a-z,A-Z,0-9, _, -]+', location, re.M|re.I)[0]
    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 = [x+'-'+y+'-01 00:00:00' for x in year for y in month ]
timepool = [x+'-'+y+'-01' for x in year for y in month ]
# print(timepool)
k = len(timepool)
for i in range(k) :
    if i < k-1 :
        beginarray = time.strptime(timepool[i], "%Y-%m-%d")
        #beginarray = time.strptime(timepool[i], "%Y-%m-%d %H:%M:%S")
        endarray = time.strptime(timepool[i+1], "%Y-%m-%d")
        #endarray = time.strptime(timepool[i+1], "%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[i]+'到'+timepool[i+1]+'共有【'+str(awemenum)+'】个视频')
           print('=============================================')
        #print(awemenum)
        for i in range(awemenum):
        # 将文件名中含有Windows下有可能会产生错误的符号进行转义,变成全角符号
            videotitle = data['aweme_list'][i]['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'][i]['video']['play_addr']['url_list'][0]
                #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处没有换行,然后 有个包 可有可无 还得下载,我给去除了。
damide 发表于 2021-8-29 23:01
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

请问这个是怎么回事?
orb001 发表于 2021-5-23 23:22
bbfh 发表于 2021-5-23 23:30
非常不错,感谢楼主分享
水蜜桃好甜 发表于 2021-5-23 23:35
感谢大佬分享
列明 发表于 2021-5-24 00:06
能不能實現自己輸入自己的號,
然後下載贊過的所有視頻?
然後下載所有關注的用戶的所有視頻?
leo文 发表于 2021-5-24 00:58
非常不错
红蓝黄 发表于 2021-5-24 02:39
感谢感谢
dszsdyx 发表于 2021-5-24 04:00
很不错。厉害!
MOEYU_VANILLA 发表于 2021-5-24 05:19
感谢分享
江南云 发表于 2021-5-24 05:32
这款软件竟然可以下载喜欢的用户视频慢慢欣赏,比较给力!
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-25 09:59

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表