pythonhtml
模塊安裝 python -m pip install chardet
python
基本語法app
字符串鏈接(若是是整型就會作加法) + 函數
函數編碼
字符串處理url
'abc'.upper() # 就字母轉成大寫 isinstance(x,str) #判斷是否爲字符串 type(str) #獲取變量類型
HTML實體轉換spa
抓網頁數據常常遇到例如>或者 這種HTML轉義符,抓到字符串裏非常煩人。 比方說一個從網頁中抓到的字符串 html = '<abc>' 用Python能夠這樣處理: import HTMLParser html_parser = HTMLParser.HTMLParser() txt = html_parser.unescape(html) #這樣就獲得了txt = '<abc>' 若是還想轉回去,能夠這樣: import cgi html = cgi.escape(txt) # 這樣又回到了 html = '<abc>'
把字符轉成url編碼code
urllib.quote("這是中文")
字符編碼處理htm
import sys #引用sys模塊進來,並非進行sys的第一次加載 reload(sys) #從新加載sys sys.setdefaultencoding('utf8') #調用setdefaultencoding函數 print sys.getdefaultencoding() #獲取系統默認編碼
經過模塊方式來獲取字符編碼 ip
import chardet chardet.detect(str)
編碼的判斷
isinstance(s, unicode) #用來判斷是否爲unicode
經過函數轉換編碼
chardet.detect(article[0]).get('encoding','utf-8')
數學函數-模塊
range(1,100) #生成之間數 注意: range(1, 100, 2) 能夠生成list [1, 3, 5, 7, 9,...]
math模塊
math.sqrt #開方
列表函數-list
L.append(x * x) #向列表裏增長元素
詞典函數-dict