首页 > 编程语言 > 详细

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

时间:2016-03-08 21:01:04      阅读:132      评论:0      收藏:0      [点我收藏+]
参考: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 = rsrc="(.+?\.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

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

原文:http://www.cnblogs.com/tdcqma/p/5255332.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!