亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Python?requests下載文件的幾種常用方法(附代碼)

 更新時(shí)間:2025年03月05日 10:56:32   作者:microhex  
這篇文章主要介紹了五種下載方式的實(shí)現(xiàn)方法,包括基礎(chǔ)下載、大文件分塊下載、帶有斷點(diǎn)續(xù)傳的下載、帶有超時(shí)和重試的下載以及完整的下載器實(shí)現(xiàn),文中給出了詳細(xì)的代碼示例,需要的朋友可以參考下

1. 基礎(chǔ)下載:

import requests

def download_file(url, save_path):
    response = requests.get(url, stream=True)
    if response.status_code == 200:
        with open(save_path, 'wb') as f:
            f.write(response.content)
        return True
    return False

# 使用示例
url = "https://example.com/file.pdf"
download_file(url, "file.pdf")


2. 大文件分塊下載:

import requests
from tqdm import tqdm

def download_large_file(url, save_path):
    response = requests.get(url, stream=True)
    if response.status_code == 200:
        file_size = int(response.headers.get('content-length', 0))
        
        # 顯示進(jìn)度條
        progress = tqdm(response.iter_content(chunk_size=8192), 
                       total=file_size,
                       unit='B', 
                       unit_scale=True)
        
        with open(save_path, 'wb') as f:
            for data in progress:
                f.write(data)
        return True
    return False

3. 帶有斷點(diǎn)續(xù)傳的下載:

import requests
import os

def resume_download(url, save_path):
    # 獲取已下載文件大小
    initial_pos = os.path.getsize(save_path) if os.path.exists(save_path) else 0
    
    # 設(shè)置 Header
    headers = {'Range': f'bytes={initial_pos}-'}
    
    response = requests.get(url, stream=True, headers=headers)
    
    # 追加模式打開(kāi)文件
    mode = 'ab' if initial_pos > 0 else 'wb'
    with open(save_path, mode) as f:
        for chunk in response.iter_content(chunk_size=8192):
            if chunk:
                f.write(chunk)

4. 帶有超時(shí)和重試的下載:

import requests
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry
import time

def download_with_retry(url, save_path, max_retries=3, timeout=30):
    session = requests.Session()
    
    # 設(shè)置重試策略
    retries = Retry(total=max_retries,
                   backoff_factor=1,
                   status_forcelist=[500, 502, 503, 504])
    
    session.mount('http://', HTTPAdapter(max_retries=retries))
    session.mount('https://', HTTPAdapter(max_retries=retries))
    
    try:
        response = session.get(url, stream=True, timeout=timeout)
        with open(save_path, 'wb') as f:
            for chunk in response.iter_content(chunk_size=8192):
                if chunk:
                    f.write(chunk)
        return True
    except Exception as e:
        print(f"Download failed: {str(e)}")
        return False


5. 完整的下載器實(shí)現(xiàn):

import requests
from tqdm import tqdm
import os
from pathlib import Path
import hashlib

class FileDownloader:
    def __init__(self, chunk_size=8192):
        self.chunk_size = chunk_size
        self.session = requests.Session()
        
    def get_file_size(self, url):
        response = self.session.head(url)
        return int(response.headers.get('content-length', 0))
    
    def get_file_hash(self, file_path):
        sha256_hash = hashlib.sha256()
        with open(file_path, "rb") as f:
            for byte_block in iter(lambda: f.read(4096), b""):
                sha256_hash.update(byte_block)
        return sha256_hash.hexdigest()
    
    def download(self, url, save_path, verify_hash=None):
        save_path = Path(save_path)
        
        # 創(chuàng)建目錄
        save_path.parent.mkdir(parents=True, exist_ok=True)
        
        # 獲取文件大小
        file_size = self.get_file_size(url)
        
        # 設(shè)置進(jìn)度條
        progress = tqdm(total=file_size,
                       unit='B',
                       unit_scale=True,
                       desc=save_path.name)
        
        try:
            response = self.session.get(url, stream=True)
            with save_path.open('wb') as f:
                for chunk in response.iter_content(chunk_size=self.chunk_size):
                    if chunk:
                        f.write(chunk)
                        progress.update(len(chunk))
            
            progress.close()
            
            # 驗(yàn)證文件完整性
            if verify_hash:
                downloaded_hash = self.get_file_hash(save_path)
                if downloaded_hash != verify_hash:
                    raise ValueError("File hash verification failed")
                    
            return True
            
        except Exception as e:
            progress.close()
            print(f"Download failed: {str(e)}")
            if save_path.exists():
                save_path.unlink()
            return False
            
    def download_multiple(self, url_list, save_dir):
        results = []
        for url in url_list:
            filename = url.split('/')[-1]
            save_path = Path(save_dir) / filename
            success = self.download(url, save_path)
            results.append({
                'url': url,
                'success': success,
                'save_path': str(save_path)
            })
        return results

# 使用示例
downloader = FileDownloader()

# 單文件下載
url = "https://example.com/file.pdf"
downloader.download(url, "downloads/file.pdf")

# 多文件下載
urls = [
    "https://example.com/file1.pdf",
    "https://example.com/file2.pdf"
]
results = downloader.download_multiple(urls, "downloads")

總結(jié) 

到此這篇關(guān)于Python requests下載文件的幾種常用方法的文章就介紹到這了,更多相關(guān)Python requests下載文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python 文本單詞提取和詞頻統(tǒng)計(jì)的實(shí)例

    python 文本單詞提取和詞頻統(tǒng)計(jì)的實(shí)例

    今天小編就為大家分享一篇python 文本單詞提取和詞頻統(tǒng)計(jì)的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-12-12
  • python使用selenium模擬瀏覽器進(jìn)入好友QQ空間留言功能

    python使用selenium模擬瀏覽器進(jìn)入好友QQ空間留言功能

    這篇文章主要介紹了python使用selenium模擬瀏覽器進(jìn)入好友QQ空間留言,在本文實(shí)現(xiàn)過(guò)程中需要注意的是留言框和發(fā)表按鈕在不同的frame,發(fā)表在外面的一層,具體實(shí)現(xiàn)過(guò)程跟隨小編一起看看吧
    2022-04-04
  • python基于opencv 實(shí)現(xiàn)圖像時(shí)鐘

    python基于opencv 實(shí)現(xiàn)圖像時(shí)鐘

    這篇文章主要介紹了python基于opencv 實(shí)現(xiàn)圖像時(shí)鐘的方法,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
    2021-01-01
  • python框架flask知識(shí)總結(jié)

    python框架flask知識(shí)總結(jié)

    今天帶大家復(fù)習(xí)python框架的相關(guān)知識(shí),文中對(duì)flask作了非常詳細(xì)的介紹,對(duì)正在學(xué)習(xí)python的小伙伴們有很好的幫助,需要的朋友可以參考下
    2021-05-05
  • Python數(shù)據(jù)可視化之用Matplotlib繪制常用圖形

    Python數(shù)據(jù)可視化之用Matplotlib繪制常用圖形

    Matplotlib能夠繪制折線圖、散點(diǎn)圖、柱狀圖、直方圖、餅圖. 我們需要知道不同的統(tǒng)計(jì)圖的意義,以此來(lái)決定選擇哪種統(tǒng)計(jì)圖來(lái)呈現(xiàn)我們的數(shù)據(jù),今天就帶大家詳細(xì)了解如何繪制這些常用圖形,需要的朋友可以參考下
    2021-06-06
  • python遞歸打印某個(gè)目錄的內(nèi)容(實(shí)例講解)

    python遞歸打印某個(gè)目錄的內(nèi)容(實(shí)例講解)

    下面小編就為大家?guī)?lái)一篇python遞歸打印某個(gè)目錄的內(nèi)容(實(shí)例講解)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-08-08
  • python?中賦值,深拷貝,淺拷貝的區(qū)別

    python?中賦值,深拷貝,淺拷貝的區(qū)別

    這篇文章主要介紹了python?中賦值,深拷貝,淺拷貝的區(qū)別,下文利用實(shí)例對(duì)三者進(jìn)行詳細(xì)的解析,具有一的的參考價(jià)值,需要的小伙伴可以參考一下,希望對(duì)你的學(xué)習(xí)有所幫助
    2022-03-03
  • python 錯(cuò)誤處理 assert詳解

    python 錯(cuò)誤處理 assert詳解

    這篇文章主要介紹了python 錯(cuò)誤處理 assert詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-04-04
  • Python3如何使用range函數(shù)替代xrange函數(shù)

    Python3如何使用range函數(shù)替代xrange函數(shù)

    這篇文章主要介紹了Python3如何使用range函數(shù)替代xrange函數(shù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-10-10
  • 在Python中操作文件之truncate()方法的使用教程

    在Python中操作文件之truncate()方法的使用教程

    這篇文章主要介紹了在Python中操作文件之truncate()方法的使用教程,是Python入門(mén)學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2015-05-05

最新評(píng)論