python抓取网页中图片并保存到本地
2016-04-02 00:00
696 查看
#-*-coding:utf-8-*- import osimport uuidimport urllib2import cookielib'''获取文件后缀名'''def get_file_extension(file): return os.path.splitext(file)[1] '''創建文件目录,并返回该目录'''def mkdir(path): # 去除左右两边的空格 path=path.strip() # 去除尾部 \符号 path=path.rstrip("\\") if not os.path.exists(path): os.makedirs(path) return path'''自动生成一个唯一的字符串,固定长度为36'''def unique_str(): return str(uuid.uuid1())'''抓取网页文件内容,保存到内存 @url 欲抓取文件 ,path+filename'''def get_file(url): try: cj=cookielib.LWPCookieJar() opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) urllib2.install_opener(opener) req=urllib2.Request(url) operate=opener.open(req) data=operate.read() return data except BaseException, e: print e return None '''保存文件到本地 @path 本地路径 @file_name 文件名 @data 文件内容'''def save_file(path, file_name, data): if data == None: return mkdir(path) if(not path.endswith("/")): path=path+"/" file=open(path+file_name, "wb") file.write(data) file.flush() file.close() #获取文件后缀名print get_file_extension("123.jpg");#創建文件目录,并返回该目录#print mkdir("d:/ljq")#自动生成一个唯一的字符串,固定长度为36print unique_str() url="http://qlogo1.store.qq.com/qzone/416501600/416501600/100?0"; save_file("d:/ljq/", "123.jpg", get_file(url))
相关文章推荐
- mysqlclient与python3.5兼容性问题
- 【Python】Python logging
- 机器学习算法的Python实现 (2):ID3决策树
- 【Python】Python startswith, endswith
- Python 自定义模块导入
- Python常用的字符串处理函数
- 练习001-002
- [译]学习IPython进行交互式计算和数据可视化(七)
- [译]学习IPython进行交互式计算和数据可视化(六)
- [译]学习IPython进行交互式计算和数据可视化(五)
- Python Requests安装
- [译]学习IPython进行交互式计算和数据可视化(四)
- python中多线程如何用Ctrl+C终止
- 人脸识别初探(Python)
- Python 练习册,每天一个小程序(0005)
- Python之re模块 —— 正则表达式操作
- python 模块 chardet下载方法及介绍
- 关于python的标准库
- Python学习(一)
- Python特殊语法:filter、map、reduce、lambda