標籤:
http://blog.csdn.net/my2010sam/article/details/14526223
---------------------
對html的解析是網頁抓取的基礎,分析抓取的結果找到自己想要的內容或標籤以達到抓取的目的。
HTMLParser是python用來解析html的模組。它可以分析出html裡面的標籤、資料等等,是一種處理html的簡便途徑。 HTMLParser採用的是一種事件驅動的模式,當HTMLParser找到一個特定的標記時,它會去調用一個使用者定義的函數,以此來通知程式處理。它主要的使用者回呼函數的命名都是以handler_開頭的,都是HTMLParser的成員函數。當我們使用時,就從HTMLParser派生出新的類,然後重新定義這幾個以handler_開頭的函數即可。這幾個函數包括:
- handle_startendtag 處理開始標籤和結束標籤
- handle_starttag 處理開始標籤,比如<xx> tag不區分大小寫
- handle_endtag 處理結束標籤,比如</xx>
- handle_charref 處理特殊字元串,就是以&#開頭的,一般是內碼錶示的字元
- handle_entityref 處理一些特殊字元,以&開頭的,比如
- handle_data 處理資料,就是<xx>data</xx>中間的那些資料
- handle_comment 處理注釋
- handle_decl 處理<!開頭的,比如<!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”
- handle_pi 處理形如<?instruction>的東西
def handle_starttag(self,tag,attr):
#注意:tag不區分大小寫,此時也可以解析 <A 標籤
# SGMLParser 會在建立attrs 時將屬性名稱轉化為小寫。
if tag==‘a‘:
for href,link in attr:
if href.lower()=="href":
pass
1. 基本解析,找到開始和結束標籤
[python] view plaincopy
- <span style="font-size:18px;">#coding:utf-8
-
- from HTMLParser import HTMLParser
- ‘‘‘‘‘
- HTMLParser的成員函數:
-
- handle_startendtag 處理開始標籤和結束標籤
- handle_starttag 處理開始標籤,比如<xx>
- handle_endtag 處理結束標籤,比如</xx>
- handle_charref 處理特殊字元串,就是以&#開頭的,一般是內碼錶示的字元
- handle_entityref 處理一些特殊字元,以&開頭的,比如
- handle_data 處理資料,就是<xx>data</xx>中間的那些資料
- handle_comment 處理注釋
- handle_decl 處理<!開頭的,比如<!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”
- handle_pi 處理形如<?instruction>的東西
-
- ‘‘‘
- class myHtmlParser(HTMLParser):
- #處理<!開頭的內容
- def handle_decl(self,decl):
- print ‘Encounter some declaration:‘+ decl
- def handle_starttag(self,tag,attrs):
- print ‘Encounter the beginning of a %s tag‘ % tag
- def handle_endtag(self,tag):
- print ‘Encounter the end of a %s tag‘ % tag
- #處理注釋
- def handle_comment(self,comment):
- print ‘Encounter some comments:‘ + comment
-
-
- if __name__==‘__main__‘:
- a = ‘<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">\
- <html><head><!--insert javaScript here!--><title>test</title><body><a href="http: //www.163.com">連結到163</a></body></html>‘
- m=myHtmlParser()
- m.feed(a)
- m.close()
-
- 輸出結果:
-
- Encounter some declaration:DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"
- Encounter the beginning of a html tag
- Encounter the beginning of a head tag
- Encounter some comments:insert javaScript here!
- Encounter the beginning of a title tag
- Encounter the end of a title tag
- Encounter the beginning of a body tag
- Encounter the beginning of a a tag
- Encounter the end of a a tag
- Encounter the end of a body tag
- Encounter the end of a html tag</span>
2. 解析html的超連結和連結顯示的內容
[python] view plaincopy
- <span style="font-size:18px;">#coding:utf-8
- from HTMLParser import HTMLParser
- class myHtmlParser(HTMLParser):
-
- def __init__(self):
- HTMLParser.__init__(self)
- self.flag=None
-
- # 這裡重新定義了處理開始標籤的函數
- def handle_starttag(self,tag,attrs):
- # 判斷標籤<a>的屬性
- if tag==‘a‘:
- self.flag=‘a‘
- for href,link in attrs:
- if href==‘href‘:
- print "href:",link
-
- def handle_data(self,data):
- if self.flag==‘a‘:
- print "data:",data.decode(‘utf-8‘)
-
- if __name__ == ‘__main__‘:
- a = ‘<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">\
- <html><head><!--insert javaScript here!--><title>test</title><body><a href="http: //www.163.com">連結到163</a></body></html>‘
- m=myHtmlParser()
- m.feed(a)
- m.close()
-
- 輸出結果:
-
- href: http: //www.163.com
- data: 連結到163</span>
或:
[python] view plaincopy
- <span style="font-size:18px;">#coding:utf-8
-
- from HTMLParser import HTMLParser
- import urllib2
-
- class myparser(HTMLParser):
-
- # 繼承父類初始化方法,並添加一個tag屬性
- def __init__(self):
- HTMLParser.__init__(self)
- self.tag = None
-
- def handle_decl(self,decl):
- print u"聲明:",decl
-
- def handle_starttag(self,tag,attrs):
- print u"開始標籤;",tag
-
- # 判斷是否是a開頭的標籤
- if tag==‘a‘ and len(attrs):
- #設定 self.tag 標記
- self.tag=‘a‘
- for href,link in attrs:
- if href==‘href‘:
- print href+":"+link
-
- def handle_endtag(self,tag):
- print u"結束標籤:",tag
-
- def handle_data(self,data):
- #處理 a 標籤開頭的資料
- if self.tag==‘a‘:
- print u"資料內容:",data.decode("utf-8")
-
- def handle_comment(self,comm):
- print u"注釋:",comm
-
-
- if __name__ == ‘__main__‘:
-
- a = ‘<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">\
- <html><head><!--insert javaScript here!--><title>test</title><body><a href="http: //www.163.com">連結到163</a><a href="http: //www.baidu.com">百度</a></body></html>‘
- m = myparser()
- m.feed(a)
-
-
-
-
-
- 結果:
-
- 聲明: DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"
- 開始標籤; html
- 開始標籤; head
- 注釋: insert javaScript here!
- 開始標籤; title
- 結束標籤: title
- 開始標籤; body
- 開始標籤; a
- href:http: //www.163.com
- 資料內容: 連結到163
- 結束標籤: a
- 開始標籤; a
- href:http: //www.baidu.com
- 資料內容: 百度
- 結束標籤: a
- 結束標籤: body
- 結束標籤: html</span>
Python之HTML的解析(網頁抓取一)