python获取图片
来源:互联网 发布:分类信息系统源码 编辑:程序博客网 时间:2024/05/22 03:03
#! /usr/bin/env python#coding:utf-8import urllib.request as requestfrom os import listdirimport refrom functools import wrapsimport timefrom bs4 import BeautifulSoup'''这个类是用来获取网站上的图片根据抓取html中的img标签中的src为url去下载图片因为目前没法下载PIL所以对于jpg, png之外的图片格式不能做预处理(((((((裁剪大小之类的PS:可以用这个来一键下载萌妹子们的图片啦!!!!一颗赛艇'''def time_caculate(func): ''' the time_caculate decorator :param func: :return: ''' i = 1 @wraps(func) def wrapper(*args, **kwargs): nonlocal i start_time = time.time() result = func(*args, **kwargs) end_time = time.time() print('the {} th call func expends {} s'.format(i, (end_time-start_time)) ) i += 1 return result return wrapper@time_caculatedef read_img_from_url(url_): return request.urlopen(url_).read()def get_avaliabel_index(filepath): ''' :param filepath: the specific path you want to read :return: the unused index of the file ''' file_name_list = {} file_list = listdir(filepath) for index, filename in enumerate(file_list, 1): if re.search('^.*\.jpg$', filename): file_name_list[index] = filename index += 1 return indexdef get_html(url_): ''' :param url_: the url you want to open with :return: the converted string content ''' response = request.urlopen(url_) html = response.read() data = str(html, encoding='utf-8') return datadef analyse(data, key, tag): ''' :param data: the converted string content which is the style of html :param key: the key of tag in the content like 'img', 'p' :param tag: the tag in the segment of key like 'src', 'alt' :return: nothing ''' soup = BeautifulSoup(data, "lxml") path = 'f:/i/' count = get_avaliabel_index(path) for list_ in soup.find_all(key): #拆分属性 dict_ = list_.attrs if tag in dict_: image = dict_[tag] img = image[image.rfind('.')::] filepath = path + str(count)+img try: with open(filepath, 'wb') as file: str_ = dict_[tag] image_data = read_img_from_url(str_) file.write(image_data) count += 1 file.close() except: passif __name__ == '__main__': url = 'http://www.moko.cc/post/1161657.html' content = get_html(url) analyse(content, key= 'img', tag= 'src')
1 0
- python自动获取图片
- python--获取网页图片
- python获取图片
- python 获取图片分辨率
- python 爬虫获取网页图片
- Python 爬虫:获取网页图片
- 通过python获取美女图片
- Python 爬虫获取百度贴吧图片
- python获取某网址下所有图片
- Python获取网上图片的方法
- Python 获取sina首页所有jpg图片
- python爬虫 分页获取图片并下载
- Python 获取百度的预览图片
- 自学python之获取bing每日图片
- 强大的Python 获取贴吧图片
- python爬虫——获取图片2
- python简单爬虫(获取图片)
- python 爬虫 获取网页中的图片
- 贪心算法的基本思想
- 大小端模式详解
- <c:forEach >中如何显示序号
- java 静态代码块 构造代码块 构造方法的执行顺序
- Windows MySQL5.7安装和配置
- python获取图片
- 结构-行为-样式-Bootstrap笔记
- 测试一个字符串中各字符出现的次数
- Quartz 2D编程指南(3)路径(Paths)
- Chrome插件会干坏事儿的
- OpenCV一个窗口显示多张图片
- 原始套接字头结构定义
- 计数排序
- Android开发中Handler的经典总结