Python_summary
生活随笔
收集整理的這篇文章主要介紹了
Python_summary
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Q: python中出現(xiàn)IndentationError:unindent does not match any outer indentation level
A:復(fù)制代碼的時候容易出現(xiàn)縮進(jìn)錯誤,雖然看起來是縮進(jìn)了,但是實際上沒有。可以用Notepad++下的
? ? ?視圖->顯示符號->顯示空格和制表符 來觀察是否縮進(jìn)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~簡單爬蟲python2.7~~~~~~~~~~~~~~~~~~~~~~~~~~
''' 簡單爬蟲 ''' #encoding:utf-8import urllib import sys import re#設(shè)置編碼 reload(sys) sys.setdefaultencoding('utf-8') #獲取系統(tǒng)編碼格式 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:已經(jīng)下載的數(shù)據(jù)塊b:數(shù)據(jù)塊的大小c:遠(yuǎn)程文件的大小'''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) #回調(diào)函數(shù)定義必須有三個參數(shù),哪怕不需要#x += 1return imglist html = getHtml("http://www.cnblogs.com/1023linlin/p/8525273.html") print getImg(html)~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
?
轉(zhuǎn)載于:https://www.cnblogs.com/1023linlin/p/8648499.html
總結(jié)
以上是生活随笔為你收集整理的Python_summary的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: centos solr 部署到 tomc
- 下一篇: 铁乐学Python_day12_作业