码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2019-03-17 14:05:51      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:multi   safari   main   视频下载   out   input   stdout   orm   highlight   

#-*-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("\r下载中: %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("\n目标页面链接地址: "))
	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(‘\n‘,‘‘)
	    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编写的带有下载进度的某页面视频下载器

标签:multi   safari   main   视频下载   out   input   stdout   orm   highlight   

原文地址:https://www.cnblogs.com/litran/p/10546492.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!