Python3下载文件或图片方法


1、使用requests

import os
import requests

def download_file(url, store_path):
    filename = url.split("/")[-1]
    filepath = os.path.join(store_path, filename)

    file_data = requests.get(url, allow_redirects=True).content
    with open(filepath, 'wb') as handler:
        handler.write(file_data)

2、使用urllib.request.urlretrieve

import os
from urllib.request import urlretrieve

def download_file(url, store_path):
    filename = url.split("/")[-1]
    filepath = os.path.join(store_path, filename)

    urlretrieve(url, filepath)

« »

1条评论

l
lililala6868 says: 回复

谢谢分享

lililala6868进行回复 取消回复

电子邮件地址不会被公开。 必填项已用*标注

昵称 *