import os
os.makedirs('./image/', exist_ok=True)
IMAGE_URL = "http://image.nationalgeographic.com.cn/2017/1122/20171122113404332.jpg"
def urllib_download():
from urllib.request import urlretrieve
urlretrieve(IMAGE_URL, './image/img1.png')
def request_download():
import requests
r = requests.get(IMAGE_URL)
with open('./image/img2.png', 'wb') as f:
f.write(r.content)
def chunk_download():
import requests
r = requests.get(IMAGE_URL, stream=True)
with open('./image/img3.png', 'wb') as f:
for chunk in r.iter_content(chunk_size=32):
f.write(chunk)
urllib_download()
print('download img1')
request_download()
print('download img2')
chunk_download()
print('download img3')模仿的案例
for img in imgList:
print('图片链接%s' %img.get("src"))
request_download(img.get("src"),'C:/htlog/'+time.strftime("%Y%m%d%H%M%S",time.localtime())+'.jpg')
def downImg(imgUrl,fileUrl):
# urllib.request.urlretrieve('http://java456.com/static/upload_image/config/logo.jpg', r'C:/htlog/9988d.jpg')
try:
urllib.request.urlretrieve(imgUrl, fileUrl)
except Exception:
print("下载失败")
def request_download(imgUrl,fileUrl):
import requests
r = requests.get(imgUrl)
with open(fileUrl, 'wb') as f:
f.write(r.content)站长微信:xiaomao0055
站长QQ:14496453