Python 下載圖片

ministep88發表於2024-03-12

下載圖片

import os
import requests
import shutil
 
def download_img(folder, name, url):
    data = get_data_from_url(url)
    save_image(folder, name, data)
 
 
def get_data_from_url(url):
    response = requests.get(url, stream=True)
    return response.raw
 
 
def save_image(folder, name, data):
    file_name = os.path.join(folder, name + '.jpg')
    with open(file_name, 'wb') as fout:
        shutil.copyfileobj(data, fout)

來源:Python 檔案操作 - 蟬時雨

相關文章