掃二維碼與項目經(jīng)理溝通
我們在微信上24小時期待你的聲音
解答本文疑問/技術咨詢/運營咨詢/技術建議/互聯(lián)網(wǎng)交流
說明

我們提供的服務有:成都網(wǎng)站建設、成都網(wǎng)站設計、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認證、兗州ssl等。為上千企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務,是有科學管理、有技術的兗州網(wǎng)站制作公司
1、HTMLparser提供了一種方便簡潔的處理html文件的方法。
它根據(jù)樹形結構將html頁面中的標簽分析成一個節(jié)點,一種類型的節(jié)點對應一個類,通過調(diào)用它可以輕松訪問標簽中的內(nèi)容。
2、html本質上是xml的子集,但是html的語法沒有html嚴格,不能用標準的DOM或者SAX來分析html。
實例
from html.parser import htmlparser
from html.entities import name2codepoint
class MyHTMLParser(HTMLParser):
def handle_starttag(self, tag, attrs):
print('<%s>' % tag)
def handle_endtag(self, tag):
print('%s>' % tag)
def handle_startendtag(self, tag, attrs):
print('<%s/>' % tag)
def handle_data(self, data):
print(data)
def handle_comment(self, data):
print('')
def handle_entityref(self, name):
print('&%s;' % name)
def handle_charref(self, name):
print('%s;' % name)
parser = MyHTMLParser()
parser.feed('''
Some html HTML tutorial...
END
''')
//test結果
Some
html
HTML tutorial...
END
以上就是python中htmlparser解析html,希望對大家有所幫助。更多Python學習指路:創(chuàng)新互聯(lián)Python教程
本文教程操作環(huán)境:windows7系統(tǒng)、Python 3.9.1,DELL G3電腦。

我們在微信上24小時期待你的聲音
解答本文疑問/技術咨詢/運營咨詢/技術建議/互聯(lián)網(wǎng)交流