Q: python中出现IndentationError:unindent does not match any outer indentation level
A:复制代码的时候容易出现缩进错误,虽然看起来是缩进了,但是实际上没有。可以用Notepad++下的
视图->显示符号->显示空格和制表符 来观察是否缩进
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~简单爬虫python2.7~~~~~~~~~~~~~~~~~~~~~~~~~~
''' 简单爬虫 ''' #encoding:utf-8 import urllib import sys import re #设置编码 reload(sys) sys.setdefaultencoding('utf-8') #获取系统编码格式 type = sys.getfilesystemencoding() def getHtml(url): page = urllib.urlopen(url) html = page.read().decode('utf-8').encode(type) return html def cbk(a,b,c): ''' a:已经下载的数据块 b:数据块的大小 c:远程文件的大小 ''' per = 100.0*a*b/c if per > 100 : per = 100 print '%.2f%%' %per def getImg(html): reg = r'src="(.+?\.jpg)" alt' imgre = re.compile(reg) imglist = re.findall(imgre,html) #x = 0 for img in imglist: local = 'c://Users/xujianjun/Desktop/python/x.jpg' #不能只包含路径,必须是路径+文件名 urllib.urlretrieve(img,local,cbk) #回调函数定义必须有三个参数,哪怕不需要 #x += 1 return imglist html = getHtml("http://www.cnblogs.com/1023linlin/p/8525273.html") print getImg(html)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~