Python-爬虫初学
2015-10-16 14:04
393 查看
#爬取网站中的图片 1 import re #正则表达式库 import urllib #url链接库 def getHtml(url): page = urllib.urlopen(url) #打开链接 html = page.read() #像读文本一样读取网页内容 return html def getImg(html): reg = r'<img src="(.+?\.png)" alt' #匹配表达式 imgre = re.compile(reg) #编译成正则表达式对象 imglist =re.findall(imgre, html) #查找全部满足匹配的 x = 0 for imgurl in imglist: print "imgurl:", imgurl urllib.urlretrieve("http://www.uestc.edu.cn/" + imgurl, '%d.png' % x) #依次遍历下载,源链接用的是相对地址,所以添加前缀 x += 1 html = getHtml("http://www.uestc.edu.cn/") print getImg(html) #print html
参考学习链接:
http://www.cnblogs.com/fnng/p/3576154.html
相关文章推荐
- [LeetCode]题解(python):027-Remove Element
- python核心编程 练习题7.5
- [LeetCode]题解(python):026-Remove Duplicates from Sorted Array
- python rodeo遇到的GConf Error问题
- Python2.7的UnicodeEncodeError: ‘ascii’ codec can’t encode 错误
- <LeetCode><Easy> 169 Majority Element
- <LeetCode><Easy> 171 Excel Sheet Column Number
- python selenium expected_conditions使用实例
- 2.Python起步(未完待续)
- 1.认识Python?
- python发送邮件(含附件)
- <LeetCode><Easy> 189 Rotate Array
- Python模块搜索路径
- 获取URL中的一级域名
- 机器学习 & python k-近邻算法
- python学习笔记 (二)
- <LeetCode><Easy> 190 Revese Bits
- Python模块中的变量和函数的作用域
- <LeetCode><Easy> 191 Number Of 1 Bit
- python __init__.py