#!/usr/bin/env python
# -*- encoding: utf-8 -*-
import urllib2
import os
def save_img(img_url,file_name,file_path='img'):
#保存图片到磁盘文件夹 file_path中,默认为当前脚本运行目录下的 book\img文件夹
try:
if not os.path.exists(file_path):
print '文件夹',file_path,'不存在,重新建立'
#os.mkdir(file_path)
os.makedirs(file_path)
#获得图片后缀
if 'jpeg' in img_url:
file_suffix = '.jpeg'
elif 'jpg' in img_url:
file_suffix = '.jpg'
elif 'png' in img_url:
file_suffix = '.png'
else:
file_suffix = '.jpeg'
#拼接图片名(包含路径)
filename = '{}{}{}{}'.format(file_path,os.path.sep,file_name,file_suffix)
#下载图片,并保存到文件夹中
response = urllib2.urlopen(img_url)
cat_img = response.read()
with open(filename, 'wb') as f:
f.write(cat_img)
except IOError as e:
print '文件操作失败',e
except Exception as e:
print '错误 :',e
save_img(img_url, file_name)

下载并保存图片Python2.7的更多相关文章

  1. Python: 下载底层由Python2转Python3环境更新手记

    谨记录运行环境改变过程中所碰到的坑. 下载底层运行环境由Python2移至Python3所遇到的问题及处理方法: 1.所引的第三方组件,基本都有替代支持:msvcr90.dll不再需要,有则报错2.引 ...

  2. APP中的图片如何长按可以下载并保存图片到相册出错处理

    https://www.cnblogs.com/interdrp/p/9302164.html 接上文. 如果用户在保存图片过程中,不小心拒绝了相册的访问权限

  3. APP中的图片如何长按可以下载并保存图片到相册

    直接上图 方式一: 实现方式二: 方式三:

  4. 如何在Mac上切换python2和python3以及下载安装包 & 在Mac上如何查找系统自带python2.7的路径

    电脑:系统是Mac OS  系统自带python2.7  自己下载安装了python3.6 问题:一开始我想在终端下执行python2的相关代码   例如 python kNN.py (kNN.py这 ...

  5. Python2和Python3在windows下共存

    Python2.7 和 Python3不兼容,两种环境可能都会用到.ubuntu14.04中已经默认安装了这两个版本,在shell中输入python会自动进入Python2.7的交互环境,输入Pyth ...

  6. 交叉编译Python-2.7.13到ARM(aarch32)平台

    作者:彭东林 邮箱:pengdonglin137@163.com QQ:405728433 环境 主机: ubuntu14.04 64bit 开发板: qemu + vexpress-a9 (参考: ...

  7. Win10下python3和python2同时安装并解决pip共存问题

    特别说明,本文是在Windows64位系统下进行的,32位系统请下载相应版本的安装包,安装方法类似. 使用python开发,环境有Python2和 python3 两种,有时候需要两种环境切换使用,下 ...

  8. CentOS6.x机器安装Python2.7.x

    准备环境:CentOS6.9机器 1.查看机器默认的Python版本 [root@hlmcent69nma ~]# python -V Python [root@hlmcent69nma ~]# wh ...

  9. 【转】Win10下 python3和python2同时安装并解决pip共存问题

    1.下载python3和python2 进入python官网,链接https://www.python.org/ 选择Downloads--->Windows,点击进入就可以看到寻找想要的pyt ...

随机推荐

  1. Git介绍及基本操作

    Git基本概念 在Git中,我们将需要进行版本控制的文件目录叫做一个仓库(repository),每个仓库可以简单理解成一个目录,这个目录里面的所有文件都通过Git来实现版本管理,Git都能跟踪并记录 ...

  2. As3 Practises : use TheMiner do as3 project swf performance profile , find memory leak!

    The second and most universal way is to launch it from the mm.cfg fileTheMiner.swf must be trusted1: ...

  3. [转]MFC 调用 printf 输出

    摘自:http://blog.csdn.net/miyunhong/article/details/6704121 #include <io.h> #include <fcntl.h ...

  4. LUAROCKS 报错解决办法

    用luarocks 加载包时报错 Warning: falling back to curl - install luasec to get native HTTPS support 此时先安装 ./ ...

  5. 瀑布流下滑 发送ajax

    <!DOCTYPE=html>      <html>      <head>      <meta charset="UTF-8"> ...

  6. HDU3507Print Article (斜率优化DP)

    Zero has an old printer that doesn't work well sometimes. As it is antique, he still like to use it ...

  7. 5款实用的硬盘、SSD固态硬盘、U盘、储存卡磁盘性能测试工具绿色版

    http://www.iplaysoft.com/disk-benchmark-tools.html/comment-page-1#comment-149425

  8. 【PS实例】轻松打造梦幻的照片

    本系列教程将开始讲解PS的一些制作实例,通过实例的讲解同时介绍各种工具和面板机快捷键的使用,这样能够让大家更有兴趣学习,在学习的同时能够创造出自己喜欢的东西.本人使用的教程都是根据本人多次调试制作,仅 ...

  9. bzoj 2770 YY的Treap

    Written with StackEdit. Description 志向远大的\(YY\)小朋友在学完快速排序之后决定学习平衡树,左思右想再加上\(SY\)的教唆,\(YY\)决定学习\(Trea ...

  10. Codeforces 589F Gourmet and Banquet

    A gourmet came into the banquet hall, where the cooks suggested n dishes for guests. The gourmet kno ...