欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > python >内容正文

python

Python_summary

发布时间:2025/3/20 python 39 豆豆
生活随笔 收集整理的这篇文章主要介绍了 Python_summary 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

Q: python中出现IndentationError:unindent does not match any outer indentation level
A:复制代码的时候容易出现缩进错误,虽然看起来是缩进了,但是实际上没有。可以用Notepad++下的
     视图->显示符号->显示空格和制表符 来观察是否缩进

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~简单爬虫python2.7~~~~~~~~~~~~~~~~~~~~~~~~~~

''' 简单爬虫 ''' #encoding:utf-8import 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 htmldef cbk(a,b,c):'''a:已经下载的数据块b:数据块的大小c:远程文件的大小'''per = 100.0*a*b/cif per > 100 :per = 100print '%.2f%%' %per def getImg(html):reg = r'src="(.+?\.jpg)" alt' imgre = re.compile(reg)imglist = re.findall(imgre,html)#x = 0for img in imglist:local = 'c://Users/xujianjun/Desktop/python/x.jpg' #不能只包含路径,必须是路径+文件名urllib.urlretrieve(img,local,cbk) #回调函数定义必须有三个参数,哪怕不需要#x += 1return imglist html = getHtml("http://www.cnblogs.com/1023linlin/p/8525273.html") print getImg(html)

  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 

转载于:https://www.cnblogs.com/1023linlin/p/8648499.html

总结

以上是生活随笔为你收集整理的Python_summary的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。