Python 批量下载图片示例
2015-07-30 17:57
609 查看
使用Python find函数和urllib下载图片。
A:
本文出自 “rabbit” 博客,请务必保留此出处http://xiajie.blog.51cto.com/6044823/1680245
A:
#!/usr/bin/env python import time import urllib i = 0 url = ['']*10 name = ['']*10 con = urllib.urlopen('http://www.ithome.com/html/bizhi/164396.htm').read() src = con.find(r'/newsuploadfiles') end = con.find(r'.jpg',src) name[0] = con[src +24 :end +1] while src != -1 and end != -1 and i < 10: url[0] = con[src -21 :end +4] src = con.find(r'/newsuploadfiles',end) end = con.find(r'.jpg',src) content = urllib.urlopen(url[0]).read() open(r'img/'+ name[0]+'jpg','w+').write(content) name[0] = con[src +24 :end +1] print url[0] time.sleep(3) i = i + 1 else: print "Download Over!"B:
#!/usr/bin/env python import re import urllib def getHtml(url): page = urllib.urlopen(url) html = page.read() return html def getImg(html): reg = r'data-original="(.+?\.jpg)" />' imgre = re.compile(reg) imglist = re.findall(imgre,html) i = 0 for imgurl in imglist: name = imgurl[45:] print name content = urllib.urlopen(imgurl).read() open(r'tmp/' + name,'w+').write(content) i = i + 1 html = getHtml("http://www.ithome.com/html/bizhi/164396.htm") print getImg(html)
本文出自 “rabbit” 博客,请务必保留此出处http://xiajie.blog.51cto.com/6044823/1680245
相关文章推荐
- mac 安装pillow模块
- Python进阶之返回函数
- python 高阶函数之自定义排序
- Python xml属性/节点/文本的增删改[xml.etree.ElementTree]
- Python文件操作注意事项
- python简单分割文件的方法
- python error python version 2.7 required,which was not found in the registry
- 用python实现链表操作
- Python读取网页内容的方法
- Python UnicodeEncodeError: 'gbk' codec can't encode character u'\ufeff' in position 0:
- python特殊注释
- python查看FTP是否能连接成功的方法
- windows 64位机器安装python机器学习环境scikit_learn
- selenium for python 所有方法
- python命名规则
- 【python】Word Jumble
- Fedora22环境下Python3.4+Django1.8+MySQL5.6环境搭建
- python基础知识
- Python 3 中检测QQ在线的脚本
- Python的getattr(),setattr(),delattr(),hasattr()及类内建__getattr__应用