import urllib
import re
import os
#urllib,re,os均为Python模块
def gethtml(outline):
page = urllib.urlopen(outline) #抓取网页内容获得图片链接
html = page.read()
return html
def getimg(html): #下载图片保存在同目录下的pictures文件夹下
reg=r‘src="(.+?\.jpg)" pic_ext‘
imgre=re.compile(reg)
imglist=imgre.findall(html)
if not imglist:
print "not found"
else:
filepath=os.getcwd() +‘\pictures‘
print filepath
if os.path.exists(filepath) is False:
os.mkdir(filepath)
global x
for imgurl in imglist:
temp = filepath + ‘\%s.jpg‘ % x
print imgurl
urllib.urlretrieve(imgurl,temp)
x=x+1x = 0
fp =file("img_path.txt") #所有网址都放在这个文件里
while True:
outline = fp.readline().strip(‘\n‘)
if len(outline)==0:
break
print outline
html=gethtml(outline)
getimg(html)fp.close()