Python(大文件下载

########## 小文件下载:

import requests
image_url = "https://openedx.obs.cn-north-1.myhwclouds.com/HuaweiX/EBGTC00000522/courseware/b6a9132e6ff311eaabf9fa163eae5130_HCIP-Cloud%20Service%20Solutions%20Architect%20V2.0%20%E5%AE%9E%E9%AA%8C%E6%89%8B%E5%86%8C.pdf?Expires=1639556511&Signature=G%2B4yKlRCK2NojEBZCEmH5AUjRAo%3D&AccessKeyId=SHDYKQQDUOBQLCW8ZLJA"
r = requests.get(image_url)
with open("python_logo.pdf",'wb') as f:
    f.write(r.content)



    ############# 大文件下载:
# 如果文件比较大的话,那么下载下来的文件先放在内存中,内存还是比较有压力的。所以为了防止内存不够用的现象出现,我们要想办法把下载的文件分块写到磁盘中
import requests
file_url = "https://openedx.obs.cn-north-1.myhwclouds.com/HuaweiX/EBGTC00000522/courseware/b6a9132e6ff311eaabf9fa163eae5130_HCIP-Cloud%20Service%20Solutions%20Architect%20V2.0%20%E5%AE%9E%E9%AA%8C%E6%89%8B%E5%86%8C.pdf?Expires=1639556511&Signature=G%2B4yKlRCK2NojEBZCEmH5AUjRAo%3D&AccessKeyId=SHDYKQQDUOBQLCW8ZLJA"
r = requests.get(file_url, stream=True)  ##stream=True流式
with open("python.pdf", "wb") as pdf:
    for chunk in r.iter_content(chunk_size=1024): ####设置大小iter_content(chunk_size=1024)
        if chunk:
            pdf.write(chunk)