import time
import subprocess
import argparse def command(cmd, timeout=60):
'''
:param cmd: 执行命令cmd,返回命令输出的内容。
:param timeout: 最长等待时间,单位:秒
:return:
'''
p = subprocess.Popen(cmd, stderr=subprocess.STDOUT, stdout=subprocess.PIPE, shell=True)
t_beginning = time.time()
while True:
if p.poll() is not None:
break
seconds_passed = time.time() - t_beginning
if timeout and seconds_passed > timeout:
p.terminate()
time.sleep(0.1)
return p.stdout.read().decode('utf-8') def run():
'''
:param cmd:
:param timeout: 默认20秒
:return: 结束 you-get: Skipping /Users/yy/Movies/斗罗大陆_06.mp4: file already exists
''' params = argparse.ArgumentParser()
params.add_argument('-u', '--url')
params.add_argument('-o', '--output', default='/Users/yy/Movies/')
params.add_argument('-t', '--timeout', default=20, type=int)
args = params.parse_args()
cmd = 'you-get {0} -o {1}'.format(args.url, args.output)
timeout = args.timeout
if args.url is None:
print('请输入下载地址')
else:
while True:
result = command(cmd, timeout)
print(result)
res = result.find('exists')
if res == -1:
print('继续下载')
else:
break if __name__ == "__main__":
run()

使用python3.7,需要安装you-get

pip3 install you-get

解决腾讯下载中断问题,此方法可以下载完整视频

05-28 20:27