使用Python编写的带有下载进度的某页面视频下载器

Posted 积水成渊

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用Python编写的带有下载进度的某页面视频下载器相关的知识,希望对你有一定的参考价值。

#-*-coding=utf-8-*-
from sys import argv
import requests
import os,re,time,random, sys

import requests,re,time,random


def download_mp4(url,filename):
    proxies = {‘http‘: ‘http://127.0.0.1:1080‘}
    headers={‘User-Agent‘:‘Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36Name‘,‘Referer‘:‘http://91.com‘,‘Content-Type‘: ‘multipart/form-data; session_language=cn_CN‘}
    path = "81born/"
    if not os.path.exists(path):
        try:
            os.makedirs(path)
        except:
            pass
    path += str(filename)+".mp4"
    print("保存位置:", path)
    file_name = str(filename)
    with open(path, "wb") as f:
        print("下载进度:", end="")
        time_pre = time.time()
        response = requests.get(url=url, headers=headers, proxies=proxies, stream=True)
        total_length = response.headers.get(‘content-length‘)

        if total_length is None: # no content length header
            f.write(response.content)
        else:
            dl = 0
            total_length = int(total_length)
            char_list = [‘\‘, ‘|‘, ‘/‘, ‘-‘ ]
            index = 0
            for data in response.iter_content(chunk_size=8192):
                dl += len(data)
                f.write(data)
                f.flush()
                done = int(40 * dl / total_length)

                time_now = time.time()
                sys.stdout.write("
下载中: %s [%s%s] %.2f%%  %.2fMB / %.2fMB SPEED: %.2fMB/s" % (char_list[index] , ‘=‘ * done, ‘ ‘ * (40-done), (dl / total_length) * 100 , dl/1000000, total_length / 1000000, ( dl/(1024.0 * 1024.0 * 1024.0) )/ ((time_now - time_pre)/1000)  ) )    
                sys.stdout.flush()
                index = (index + 1) % len(char_list)





def random_ip():
    a=random.randint(1,255)
    b=random.randint(1,255)
    c=random.randint(1,255)
    d=random.randint(1,255)
    return(str(a)+‘.‘+str(b)+‘.‘+str(c)+‘.‘+str(d))

def main():

	headers={‘Accept-Language‘:‘zh-CN,zh;q=0.9‘,‘User-Agent‘: ‘Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36‘,‘X-Forwarded-For‘:random_ip(),‘referer‘: r‘http://91porn.com/‘,‘Content-Type‘: ‘multipart/form-data; session_language=cn_CN‘}
	url= str(input("
目标页面链接地址: "))
	video_url=[]
	proxies = {‘http‘: ‘http://127.0.0.1:1080‘}
	base_req=requests.get(url=url,headers=headers,proxies=proxies)
	tittle=re.findall(r‘<div id="viewvideo-title">(.*?)</div>‘,str(base_req.content,‘utf-8‘,errors=‘ignore‘),re.S)
	t = None
	try:
	    t=tittle[0]
	    tittle[0]=t.replace(‘
‘,‘‘)
	    t=tittle[0].replace(‘ ‘,‘‘)
	except IndexError:
	    pass

	video_url = re.findall(r‘<source src="(.*?)" type=‘video/mp4‘>‘,str(base_req.content,‘utf-8‘,errors=‘ignore‘))
	print(">> 开始下载")
	download_mp4(video_url[0], t)

if __name__ == "__main__":
	try:
		while True:
			main()
	except Exception as e:
		print(e)
	finally:
		pass

  主要是使用stdout.write 和 flush方法实现了一个控制台信息尾部刷新的效果

以上是关于使用Python编写的带有下载进度的某页面视频下载器的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Python 中编写下载进度指示器?

如何在 Python 中编写下载进度指示器?

C# 下载带有进度指示器的文件,然后安装文件

带有进度条下载更新的自定义 UITableViewCell

如何使用 Ktor 和 Kotlin 下载带有进度指示器的大文件?

网页插入SWF视频,如何带有播放控制按钮、进度条等!