python脚本工具-1 制作爬虫下载网页图片

参考:http://www.cnblogs.com/fnng/p/3576154.html

本文参考虫师的博客“python实现简单爬虫功能”,整理分析后抓取其他站点的图片并下载保存在本地。

  • 抓取图片等网址:http://www.cnblogs.com/fnng/p/3576154.html
  • 用到的正则表达式:reg = r'src="(.+?\.png)"'
  1. 源代码:
 1 #! /usr/bin/python
 2 # coding:utf-8
 3 
 4 #导入urllib与re模块
 5 import urllib 
 6 import re
 7 
 8 # 定义一个函数获片取页面的信息,返回html文件。
 9 def getHtml(url):
10   page = urllib.urlopen(url)
11   html = page.read()
12   return html
13 
14 #将页面中的图片保存为正则表达式对象,通过for循环,
15 #利用urllib.urlretrieve()方法将所有图片下载到本地。
16 def getImg(html):
17     reg = r'src="(.+?\.png)"'
18     imgre = re.compile(reg)
19     imglist = re.findall(imgre,html)
20     x = 0
21     for imgurl in imglist:
22       urllib.urlretrieve(imgurl,'%s.png' % x)
23       x+=1
24 
25 html = getHtml("http://www.cnblogs.com/fnng/p/3576154.html")

  2. 终端下看到的已下载好的图片

spdbmadeMacBook-Pro:crawler spdbma$ ls
0.png        2.png        4.png        6.png
1.png        3.png        5.png        getjpg.py
posted @ 2016-03-08 19:46  北海悟空  阅读(669)  评论(0编辑  收藏  举报