欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Python 下载文件

程序员文章站 2022-03-02 12:48:42
...
import urllib.request

urllib.request.urlretrieve('http://...hxbl.rar','hxbl.rar')


from urllib import request, error
import time
headers = {
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
    "Accept-Encoding": "gzip, deflate",
    "Accept-Language": "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2",
    "Connection": "keep-alive",
    "Cache-Control": "no-cache",
    "Cookie": "safedog-flow-item=9DF4632D170F8DE432727A099F103A9C; __tins__271340=%7B%22sid%22%3A%201527747066938%2C%20%22vd%22%3A%2036%2C%20%22expires%22%3A%201527750624194%7D; __51cke__=; __51laig__=57; Hm_lvt_19cd191a3f9da0f4907143f1afbd8bd3=1527747067; Hm_lpvt_19cd191a3f9da0f4907143f1afbd8bd3=1527748824; ASPSESSIONIDCQCTSTBQ=BNJDIIGDMCFNCCILFBKJJBLP; bdshare_firstime=1527747074640; __tins__15659891=%7B%22sid%22%3A%201527747078608%2C%20%22vd%22%3A%2021%2C%20%22expires%22%3A%201527750624530%7D",
    #"Host": "www.....com",
    "Pragma": "no-cache",
    "Referer": "http://www.....com/xx.asp?ID=36165",
    "Upgrade-Insecure-Requests": "1",
    "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:59.0) Gecko/20100101 Firefox/59.0"

}


for i in range(36259,1,-1):
    print(i)
    url = "http://www.....com/xx.asp?ID="+str(i)+"&sID=0"
    req = request.Request(url=url, headers=headers, method='GET')
    try:
        response = request.urlopen(req)
        content = response.read()
        # encoding = response.info()
        # print(type(content))
        # print(content)
        with open('C:\\Elag\\video\\'+str(i)+'.rar', "wb") as file:
            file.write(content)
    except error.HTTPError as e:
        print(e.code)
        print(e.reason)
        print(e)
        time.sleep(1)

电影下载

# -*- coding:utf-8 -*-
import urllib.request
import requests
from contextlib import closing

class ProgressBar(object):
    def __init__(self, title, count=0.0, run_status=None, fin_status=None, total=100.0, unit='', sep='/',
                 chunk_size=1.0):
        super(ProgressBar, self).__init__()
        self.info = "[%s] %s %.2f %s %s %.2f %s"
        self.title = title
        self.total = total
        self.count = count
        self.chunk_size = chunk_size
        self.status = run_status or ""
        self.fin_status = fin_status or " " * len(self.statue)
        self.unit = unit
        self.seq = sep

    def __get_info(self):
        # 【名称】状态 进度 单位 分割线 总数 单位
        _info = self.info % (
            self.title, self.status, self.count / self.chunk_size, self.unit, self.seq, self.total / self.chunk_size,
            self.unit)
        return _info

    def refresh(self, count=1, status=None):
        self.count += count
        # if status is not None:
        self.status = status or self.status
        end_str = "\r"
        if self.count >= self.total:
            end_str = '\n'
            self.status = status or self.fin_status
        print(self.__get_info(), end=end_str)

#urllib.request.urlretrieve('http://dl189.80s.im:920/1806/[冰雪女王3:火与冰]BD国语中字/[冰雪女王3:火与冰]BD国语中字.mp4','bxnw.mp4')
url='http://dl189.80s.im:920/1806/[冰雪女王3:火与冰]BD国语中字/[冰雪女王3:火与冰]BD国语中字.mp4'
with closing(requests.get(url, stream=True)) as response:
    chunk_size = 1024
    content_size = int(response.headers['content-length'])
    file_D='bxnw.mp4'
    progress = ProgressBar('bxnw', total=content_size, unit="KB", chunk_size=chunk_size, run_status="正在下载",fin_status="下载完成")
    with open(file_D, "wb") as file:
        for data in response.iter_content(chunk_size=chunk_size):
            file.write(data)
            progress.refresh(count=len(data))