Python 使用 requests 下载网络文件

1959 0

 

import requests

def download(netPath,localPath,fileName):
r = requests.get(netPath)
with open(localPath + fileName, "wb") as code:
code.write(r.content)

#网络文件连接
netPath = "http://xujoe.com/favicon.ico"
#本地存储路径
localPath = "D:\Python\web_file/"
#文件名
fileName = "1.ico"
download(netPath,localPath,fileName)

去掉fileName使用网络名称

import requests
def download(netPath,localPath):
    #分割
    split = netPath.split('/')
    #获取最后一个元素
    fileName = split[len(split)-1]
    r = requests.get(netPath)
    with open(localPath+fileName, "wb") as code:
        code.write(r.content)

#网络文件连接
netPath = "http://xujoe.com/favicon.ico"
#本地存储路径
localPath = "D:\Python\web_file/"
download(netPath,localPath)

 

 

发表回复