废话不多说,直接上源码。
import os,urllib,urllib2,re path='E:/img' #图片保存路径 url=raw_input('Please input URL:')#贴吧地址 pn=1 #贴吧页码 while 1: address=url+'?'+'pn='+str(pn)#当前页地址 try: html=urllib2.urlopen(address).read()#读取当前页html print '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!page%d!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'%(pn) except: print 'TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT' exit(0) m=re.findall('http://imgsrc.baidu.com/forum/w%3D580/sign=.*?.jpg',html)#利用正则表达式分析图片URL for i in range(0,len(m)): print m[i] n=re.search('sign=(.*?)/',m[i])#提取图片URL部分字符串作为文件名 file_name=n.group(1)+'.jpg' file_name=os.path.join(path,file_name) try: print 'loading...' urllib.urlretrieve(m[i],file_name)#下载图片 print 'this picture down complete.' except: print 'error' pn=pn+1#翻页