python脚本工具-1 制作爬虫下载网页图片
2016-03-08 19:46
841 查看
参考:/article/4666295.html
本文参考虫师的博客“python实现简单爬虫功能”,整理分析后抓取其他站点的图片并下载保存在本地。
抓取图片等网址:/article/4666295.html
用到的正则表达式:reg = r'src="(.+?\.png)"'
源代码:
#! /usr/bin/python # coding:utf-8 #导入urllib与re模块 import urllib import re # 定义一个函数获片取页面的信息,返回html文件。 def getHtml(url): page = urllib.urlopen(url) html = page.read() return html #将页面中的图片保存为正则表达式对象,通过for循环, #利用urllib.urlretrieve()方法将所有图片下载到本地。 def getImg(html): reg = r'src="(.+?\.png)"' imgre = re.compile(reg) imglist = re.findall(imgre,html) x = 0 for imgurl in imglist: urllib.urlretrieve(imgurl,'%s.png' % x) x+=1 html = getHtml("/article/4666295.html")
2. 终端下看到的已下载好的图片
spdbmadeMacBook-Pro:crawler spdbma$ ls 0.png 2.png 4.png 6.png 1.png 3.png 5.png getjpg.py
相关文章推荐
- ansible api python joke
- python第三方库系列之十九--python測试使用的mock库
- 基于Python的流式数据分析平台
- 【FastDFS分布式文件系统之二】:FastDFS小文件上传性能测试及Python客户端上传操作
- [Audio processing] 数据集生成 & 性别年龄分类训练 Python
- [转]Python os.path模块
- 学习python前需要了解的东西——————-python学习流程和资源
- PAT basic 1036 python
- Python变量作用域
- Python学习之:打印输出同一行
- 【零基础入门学习Python笔记001】我和Poyhon的第一次亲密接触:课后测试集答案
- Python Unicode and str
- python suds模块实现webservice客户端通讯
- 一个简单的爬虫程序
- python入门(三)
- Python Paramiko模块安装和使用
- python多版本管理
- lda模型的python实现
- [基础] Python问题
- Python实现动态规划切割钢条问题