python抓取网页中图片并保存到本地
来源:互联网 发布:怎样在电脑上开淘宝店 编辑:程序博客网 时间:2024/04/29 13:04
#-*-coding:utf-8-*-
import os
import uuid
import urllib2
import 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)q
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")
#自动生成一个唯一的字符串,固定长度为36
print unique_str()
url="http://qlogo1.store.qq.com/qzone/416501600/416501600/100?0";
save_file("d:/ljq/", "123.jpg", get_file(url))
import os
import uuid
import urllib2
import 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)q
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")
#自动生成一个唯一的字符串,固定长度为36
print unique_str()
url="http://qlogo1.store.qq.com/qzone/416501600/416501600/100?0";
save_file("d:/ljq/", "123.jpg", get_file(url))
阅读全文
0 0
- python抓取网页中图片并保存到本地
- python抓取网页中图片并保存到本地
- python 解析网页,将抓取到的图片保存到本地
- python 保存网页图片到本地
- JAVA抓取网页图片并下载到本地
- Python 爬虫抓取美女图片保存到本地
- python爬虫之抓取网页中的图片到本地
- 利用python正则表达式抓取网页中的图片到本地
- 黄聪:python访问抓取网页常用命令(保存图片到本地、模拟POST、GET、中文编码问题)
- python抓取网站的图片并下载到本地
- python 抓取今日头条街拍图片并下载到本地
- 简单分析抓取图片并保存本地
- python 爬取csdn网页并保存博客到本地
- 抓取远程文件并保存到本地
- java 抓取网页图片并保存
- C#抓取网络图片保存到本地
- PHP抓取远程图片到本地保存
- 抓取远程图片保存到本地
- 推荐几个精致的前端web UI框架
- 大数相加
- kotlin集合操作符——总数操作符
- ES6 箭头函数链式调用
- 谈一谈java中对象的排序
- python抓取网页中图片并保存到本地
- [SCOI2005]最大子矩阵
- SSD: Single Shot MultiBox Detector
- 客户端配置文件优化策略
- CSDN全屏编辑文章
- 获取在线帮助
- 数据结构学习笔记(9)---已知二叉树的遍历序列,恢复二叉树
- Yii2中hasMany用法示例
- java连接虚拟机中的redis服务