from HTMLParser import HTMLParser
class MyHTMLParser(HTMLParser):
def handle_starttag(self, tag, attrs):
print "Encountered the beginning of a %s tag" % tag
def handle_endtag(self, tag):
print "Encountered the end of a %s tag" % tag
def handle_data(self, data):
print "Encountered data %s" % data
p = MyHTMLParser()
p.feed('<p>test</p>')
-
Encountered the beginning of a p tag
Encountered data test
Encountered the end of a p tag
我从docs扩展了示例:
-
根据@tauran发布的内容,你可能想做这样的事情:
现在,您可能希望将所有
<p>
内容推送到一个列表中,并作为结果或类似的其他内容返回该列表。TIL:在使用这样的库时,您需要考虑使用堆栈!
它似乎对我的代码不起作用,所以我像一种全局变量一样在外部定义了
tag_stack = []
。相关问题 更多 >
编程相关推荐