完整pytyon的代码如下:
#coding=utf-8
#urllib模块提供了读取Web页面数据的接口
import urllib
#re模块主要包含了正则表达式
import re
import random
import 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 imgList
writeImgList(imgList,0)