import urllib2,urllib
import os,re
if __name__ == '__main__':
pathw = os.getcwd()
for i in range(5, 5365 + 1):
try:
response = urllib2.urlopen('http://www.cssmoban.com/cssthemes/'+str(i)+'.shtml')
html = response.read()
imagetitleregion = r'<div class="large-Imgs">\r\n <img src="(.+?)" alt="(.+?)">'
print imagetitleregion
#提取图片和标题
m = re.compile(imagetitleregion).findall(html,re.S)
imageurl = m[0][0].decode("utf-8")
title = m[0][1].decode("utf-8")
path = pathw+'\\'+title+'_'+str(i)
#print imageurl
if not(os.path.isdir(path)):
os.mkdir(path)
if imageurl != u'佚名':
urllib.urlretrieve('http://www.cssmoban.com'+imageurl, path+'\\'+str(i)+'.jpg')
#提取文件路径
downregion = '<a href="(.+?)" target="_blank" class="button btn-down" title="免费下载"><i class="icon-down icon-white"></i><i class="icon-white icon-down-transiton"></i>免费下载</a>'
m = re.compile(downregion).findall(html)
urllib.urlretrieve(m[0], path+'\\'+str(i)+'.rar')
except:
print '爬页面数据失败'
代码贴出来了