python download

如何优雅的下载图片:

1.最原始野性:

import requests

img_url = "www.tupian/xxxxx.png"

img_read = requests.get(img_url).content

with open('img_file.jpg','wb') as f:
    
    f.write(img_read)

2.含蓄

import urllib.request

img_url = "www.tupian/xxxxx.png"

urllib.request.urlretrieve(img_url ,"img_file.png")

3.优雅

import wget

img_url = "www.tupian/xxxxx.png"

wget.download(img_url ,"img_file.jpg")

4.逼格

import requests

from PIL import Image

from io import BytesIO

img_url = "www.tupian/xxxxx.png"

response = requests.get(img_url)

image = Image.open(BytesIO(response.content))

image.save("img_file.jpg")
os.makedirs("D:/ai/"+img_dir, exist_ok=True)  #创建图片父文件夹

你可能感兴趣的:(工具教程)