新聞中心
說明

我們提供的服務(wù)有:成都網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計(jì)、微信公眾號(hào)開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、兗州ssl等。為上千企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的兗州網(wǎng)站制作公司
1、HTMLparser提供了一種方便簡潔的處理html文件的方法。
它根據(jù)樹形結(jié)構(gòu)將html頁面中的標(biāo)簽分析成一個(gè)節(jié)點(diǎn),一種類型的節(jié)點(diǎn)對(duì)應(yīng)一個(gè)類,通過調(diào)用它可以輕松訪問標(biāo)簽中的內(nèi)容。
2、html本質(zhì)上是xml的子集,但是html的語法沒有html嚴(yán)格,不能用標(biāo)準(zhǔn)的DOM或者SAX來分析html。
實(shí)例
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結(jié)果
Some
html
HTML tutorial...
END
以上就是python中htmlparser解析html,希望對(duì)大家有所幫助。更多Python學(xué)習(xí)指路:創(chuàng)新互聯(lián)Python教程
本文教程操作環(huán)境:windows7系統(tǒng)、Python 3.9.1,DELL G3電腦。
網(wǎng)站欄目:創(chuàng)新互聯(lián)Python教程:python中htmlparser解析html
文章URL:http://m.fisionsoft.com.cn/article/dhgssec.html


咨詢
建站咨詢
