python3 爬虫教程
1、在python文件中定义一个获取html文件流的函数def getHtml(url): page = urllib.urlopen(url) #urllib.urlopen()方法用于打开一个URL地址 html = page.read() #read()方法用于读取URL上的数据 return html
2、在python中定义在文件流中查找图像对象的函数,需要使用到正则表达式进行图像文件的匹配。def getImgList(html): print html reg = r'http://.+?\.jpg' imgre = re.compile(reg) imglist = re.findall(imgre,html) print "imglist:%s" %imglist return imglist
3、定义获取到的图像文件下载到本地硬盘的函数,使用随机数生成文件名避免文件名冲突。#将文件列表写入磁盘颊俄岿髭def writeImgList(imglist,start): x = start for imgurl in imglist: print "begin write the image %s" %imgurl fileName = 'e:\jiandan\%s.jpg' %random.randint(100000, 9999999) if os.path.exists(fileName): fileName = 'e:\jiandan\%s.jpg' %random.randint(100000, 9999999) #urllib.urlretrieve(imgurl,fileName) urllib.urlretrieve(imgurl,'%s' %fileName) x+=1
4、输入一个网址进行图像文件的查找并下载到本地url = "http://www.moko.cc/channels/post/23/1.html"imgList = getImgList(getHtml(url))print imgListwriteImgList(imgList,0)
5、完整pytyon的代码如下:#coding=utf-8#urllib模块提供了读取Web页面数据的接口import urllib#re模块主要包含了正嬴猹缥犴则表达式import reimport randomimport os#定义一个getHtml()函数def getHtml(url): page = urllib.urlopen(url) #urllib.urlopen()方法用于打开一个URL地址 html = page.read() #read()方法用于读取URL上的数据 return html#搜索图片文件def getImgList(html): print html #http://wx2.sinaimg.cn/mw600/006GlaT2ly1fdaip4dnmpj30hq0qo0vl.jpg reg = r'http://.+?\.jpg' imgre = re.compile(reg) imglist = re.findall(imgre,html) print "imglist:%s" %imglist return imglist #将文件列表写入磁盘def writeImgList(imglist,start): x = start for imgurl in imglist: print "begin write the image %s" %imgurl fileName = 'e:\jiandan\%s.jpg' %random.randint(100000, 9999999) if os.path.exists(fileName): fileName = 'e:\jiandan\%s.jpg' %random.randint(100000, 9999999) #urllib.urlretrieve(imgurl,fileName) urllib.urlretrieve(imgurl,'%s' %fileName) x+=1#url为搜索图片文件的网页地址url = "http://www.moko.cc/channels/post/23/1.html"imgList = getImgList(getHtml(url))print imgListwriteImgList(imgList,0)
6、运行tupian.py文件进行图片查找并下载
