python将网页上的图片保存至本地

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import os, sys, time
import urllib.request, requests, bs4

url = 'https://img.zcool.cn/community/0133545d90afc5a8012060bef6e4c0.gif'
fname = url.split('/')[-1] #截取文件名
imgres = requests.get(url) #取得文件内容
with open("D:\\360Downloads\\{}".format(fname), "wb") as f:
   f.write(imgres.content)
os._exit(0)

存放至指定的test目录

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import os, sys, time
import urllib.request, requests, bs4

folder = 'D:\\360Downloads\\test\\'
if not os.path.exists(folder):
   os.mkdir(folder)

url = 'https://img.zcool.cn/community/0133545d90afc5a8012060bef6e4c0.gif'
fname = url.split('/')[-1]
imgres = requests.get(url)
with open(folder + fname, "wb") as f:
   f.write(imgres.content)
os._exit(0)

你可能感兴趣的:(python)