PythonHTMLPars

2024-04-29 01:29:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用HTMLParser解析一个html文档,我想打印p标记开始和结束之间的内容

查看我的代码片段

    def handle_starttag(self, tag, attrs):
        if tag == 'p':
            print "TODO: print the contents"

Tags: 代码文档标记self内容ifdefhtml
3条回答

我从docs扩展了示例:

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

根据@tauran发布的内容,你可能想做这样的事情:

from HTMLParser import HTMLParser

class MyHTMLParser(HTMLParser):
    def print_p_contents(self, html):
        self.tag_stack = []
        self.feed(html)

    def handle_starttag(self, tag, attrs):
        self.tag_stack.append(tag.lower())

    def handle_endtag(self, tag):
        self.tag_stack.pop()

    def handle_data(self, data):
        if self.tag_stack[-1] == 'p':
            print data

p = MyHTMLParser()
p.print_p_contents('<p>test</p>')

现在,您可能希望将所有<p>内容推送到一个列表中,并作为结果或类似的其他内容返回该列表。

TIL:在使用这样的库时,您需要考虑使用堆栈

它似乎对我的代码不起作用,所以我像一种全局变量一样在外部定义了tag_stack = []

from html.parser import HTMLParser
    tag_stack = []
    class MONanalyseur(HTMLParser):

    def handle_starttag(self, tag, attrs):
        tag_stack.append(tag.lower())
    def handle_endtag(self, tag):
        tag_stack.pop()
    def handle_data(self, data):
        if tag_stack[-1] == 'head':
            print(data)

parser=MONanalyseur()
parser.feed()    

相关问题 更多 >