1. 程式人生 > >python多執行緒爬蟲學習--去除html的標籤

python多執行緒爬蟲學習--去除html的標籤

import re
import urllib

page = urllib.urlopen("http://www.baidu.com")
html  = page.read()

pattern = re.compile(r'<[^>]+>', re.S)
result = pattern.sub('', html)

print result

re.compile返回的是一個正則的表達的的樣式,後面會根據這個樣式進行去除
關鍵地帶在"r'<[^>]+>'",這個是表示以'<'開始的,'[^>]'匹配除去'>'符號的所有其他符號,'+'表示出現次數為1次或者無限次。